推荐学习书目
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
MrgHOST
V2EX  ›  Python

Python 相关问题

  •  
  •   MrgHOST · Sep 11, 2017 · 3554 views
    This topic created in 3211 days ago, the information mentioned may be changed or developed.

    ‘ 7w876543217w'怎么拆分成[7w,87,65,43,21,7w]

    list['7w876543217w']貌似不行

    12 replies    2017-09-13 00:41:14 +08:00
    Cooky
        1
    Cooky  
       Sep 11, 2017 via Android
    [ list[i,I+2] for i in range(len(list)) ]
    Cooky
        2
    Cooky  
       Sep 11, 2017 via Android   ❤️ 1
    @Cooky 第一个逗号改冒号,切片
    MrgHOST
        3
    MrgHOST  
    OP
       Sep 11, 2017
    @Cooky 谢谢
    qlbr
        4
    qlbr  
       Sep 11, 2017   ❤️ 1
    >>> import re
    >>> string='7w876543217w'
    >>> re.findall('.{2}',string)
    ['7w', '87', '65', '43', '21', '7w']
    Cooky
        5
    Cooky  
       Sep 11, 2017 via Android   ❤️ 1
    @MrgHOST 直接用 str 切片就行,转 list 多余了…
    nauynah
        6
    nauynah  
       Sep 11, 2017 via iPhone   ❤️ 2
    >>> [str[i: i+2] for i in range(0, 10, 2)]
    acheapskate
        7
    acheapskate  
       Sep 11, 2017   ❤️ 1
    s = '7w876543217w'
    l = [m + n for m,n in zip(s[0::2],s[1::2])]
    acheapskate
        8
    acheapskate  
       Sep 11, 2017
    @nauynah range(0, 11, 2) ,不然截取不到最后 2 个字符 。感觉用 range 分隔还是最明晰方便的
    nauynah
        9
    nauynah  
       Sep 11, 2017
    @acheapskate 是的,我把字符串长度看成 10 了
    40huo
        10
    40huo  
       Sep 11, 2017   ❤️ 1
    [string[x : x + width] for x in range(0, len(string), width)]
    yucongo
        11
    yucongo  
       Sep 13, 2017
    In [538]: [elm + s[1::2][idx] for idx, elm in enumerate(s[::2])]
    Out[538]: ['7w', '87', '65', '43', '21', '7w']
    yucongo
        12
    yucongo  
       Sep 13, 2017
    In [544]: s
    Out[544]: '7w876543217w'

    In [545]: [ s[2*idx: 2*idx+2] for idx in range(len(s)//2)]
    Out[545]: ['7w', '87', '65', '43', '21', '7w']
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1403 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 16:55 · PVG 00:55 · LAX 09:55 · JFK 12:55
    ♥ Do have faith in what you're doing.