推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Zhepro
V2EX  ›  Python

复制出来四个数字中间带换行,怎么用 Python 处理呢?

  •  
  •   Zhepro · May 30, 2020 · 2829 views
    This topic created in 2199 days ago, the information mentioned may be changed or developed.
    复制到代码里还是换行运行不了,怎么把换行去掉四个数字相加?
    Sanko
        1
    Sanko  
       May 30, 2020
    给个例子
    Zhepro
        2
    Zhepro  
    OP
       May 30, 2020
    @Sanko
    print(splitss('66
    82
    124
    125'))
    就是我从网页复制四个数字到代码里就是上面这样自带换行,实际上应该怎么处理呢,就是把换行去掉相加
    tom8
        3
    tom8  
       May 30, 2020
    def splitss(str):
    sum = 0
    for i in str.split('\n'):
    sum += int(i)
    return sum
    ipwx
        4
    ipwx  
       May 30, 2020
    S = '''66
    82
    124
    125'''

    print(sum(map(int, S.split('\n'))))
    ----

    话说但凡看完了 Python 教程都不至于不会写这个吧?
    Zhepro
        5
    Zhepro  
    OP
       May 30, 2020
    @ipwx 。。。小白,没想到这条
    Zhepro
        6
    Zhepro  
    OP
       May 30, 2020
    @tom8 谢谢,搞定了
    yucongo
        7
    yucongo  
       May 30, 2020
    sum(int(elm) for elm in S.split())

    sum(int(elm) for elm in re.findall(r'\d+', S))
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2732 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 15:22 · PVG 23:22 · LAX 08:22 · JFK 11:22
    ♥ Do have faith in what you're doing.