Is there a way to split a string by every nth separator in Python?

前端 未结 6 1964
时光取名叫无心
时光取名叫无心 2020-12-05 23:17

For example, if I had the following string:

\"this-is-a-string\"

Could I split it by every 2nd \"-\" rather than every \"-\" so that it returns two values (\

6条回答
  •  伪装坚强ぢ
    2020-12-06 00:06

    >>> s="a-b-c-d-e-f-g-h-i-j-k-l"         # use zip(*[i]*n)
    >>> i=iter(s.split('-'))                # for the nth case    
    >>> map("-".join,zip(i,i))    
    ['a-b', 'c-d', 'e-f', 'g-h', 'i-j', 'k-l']
    
    >>> i=iter(s.split('-'))
    >>> map("-".join,zip(*[i]*3))
    ['a-b-c', 'd-e-f', 'g-h-i', 'j-k-l']
    >>> i=iter(s.split('-'))
    >>> map("-".join,zip(*[i]*4))
    ['a-b-c-d', 'e-f-g-h', 'i-j-k-l']
    

    Sometimes itertools.izip is faster as you can see in the results

    >>> from itertools import izip
    >>> s="a-b-c-d-e-f-g-h-i-j-k-l"
    >>> i=iter(s.split("-"))
    >>> ["-".join(x) for x in izip(i,i)]
    ['a-b', 'c-d', 'e-f', 'g-h', 'i-j', 'k-l']
    

    Here is a version that sort of works with an odd number of parts depending what output you desire in that case. You might prefer to trim the '-' off the end of the last element with .rstrip('-') for example.

    >>> from itertools import izip_longest
    >>> s="a-b-c-d-e-f-g-h-i-j-k-l-m"
    >>> i=iter(s.split('-'))
    >>> map("-".join,izip_longest(i,i,fillvalue=""))
    ['a-b', 'c-d', 'e-f', 'g-h', 'i-j', 'k-l', 'm-']
    

    Here are some timings

    $ python -m timeit -s 'import re;r=re.compile("[^-]+-[^-]+");s="a-b-c-d-e-f-g-h-i-j-k-l"' 'r.findall(s)'
    100000 loops, best of 3: 4.31 usec per loop
    
    $ python -m timeit -s 'from itertools import izip;s="a-b-c-d-e-f-g-h-i-j-k-l"' 'i=iter(s.split("-"));["-".join(x) for x in izip(i,i)]'
    100000 loops, best of 3: 5.41 usec per loop
    
    $ python -m timeit -s 's="a-b-c-d-e-f-g-h-i-j-k-l"' 'i=iter(s.split("-"));["-".join(x) for x in zip(i,i)]'
    100000 loops, best of 3: 7.3 usec per loop
    
    $ python -m timeit -s 's="a-b-c-d-e-f-g-h-i-j-k-l"' 't=s.split("-");["-".join(t[i:i+2]) for i in range(0, len(t), 2)]'
    100000 loops, best of 3: 7.49 usec per loop
    
    $ python -m timeit -s 's="a-b-c-d-e-f-g-h-i-j-k-l"' '["-".join([x,y]) for x,y in zip(s.split("-")[::2], s.split("-")[1::2])]'
    100000 loops, best of 3: 9.51 usec per loop
    

提交回复
热议问题