V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
ioiioi
V2EX  ›  Python

如何截取字符串?

  •  
  •   ioiioi · Jul 15, 2013 · 3887 views
    This topic created in 4682 days ago, the information mentioned may be changed or developed.
    假设有一个tuple为[['128 MB', '27 PERCENT']]
    我想将128和27全部转换成int,该怎么截取字符串?
    4 replies    1970-01-01 08:00:00 +08:00
    mrluanma
        1
    mrluanma  
       Jul 15, 2013
    >>> [int(x.split(None, 1)[0]) for x in ['128 MB', '27 PERCENT']]
    [128, 27]
    ioiioi
        2
    ioiioi  
    OP
       Jul 15, 2013
    @mrluanma
    perfect
    有个疑问,split(None, 1)是啥意思?
    mrluanma
        3
    mrluanma  
       Jul 15, 2013
    @ioiioi

    >>> help("".split)

    Help on built-in function split:

    split(...)
    S.split([sep [,maxsplit]]) -> list of strings

    Return a list of the words in the string S, using sep as the
    delimiter string. If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified or is None, any
    whitespace string is a separator and empty strings are removed
    from the result.
    ToughGuy
        4
    ToughGuy  
       Jul 16, 2013
    import re

    [map(int, re.findall(r'\d+', x)) for x in ['128 MB', '27 PERCENT']]

    新手...
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1671 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 16:20 · PVG 00:20 · LAX 09:20 · JFK 12:20
    ♥ Do have faith in what you're doing.