2014-02-08 58 views
3

如果我有数据存储在列表中,例如如何将列表元素拆分为两个?

images = ['pdf-one','gif-two','jpg-three'] 

如何在连字符中将这些元素拆分为多个元素 - 而不是子列表。即

images = ['pdf','-one','gif','-two','jpg','-three'] 

images = [['pdf','-one'],['gif','-two'],['jpg','-three']] 
+0

包括连字符? –

+0

是的,它需要识别连字符,并从那里开始分割字符串 –

+1

如果有多个连字符会发生什么? –

回答

5

在用正则表达式这种情况下分裂使得对最可读的代码:

import re 

hyphensplit = re.compile('(-[a-z]+)').split 
images = [part for img in images for part in hyphensplit(img) if part] 

演示:

>>> import re 
>>> hyphensplit = re.compile('(-[a-z]+)').split 
>>> images = ['pdf-one','gif-two','jpg-three'] 
>>> [part for img in images for part in hyphensplit(img) if part] 
['pdf', '-one', 'gif', '-two', 'jpg', '-three'] 
4

您可以使用str.partition为这个:

>>> from itertools import chain 
>>> images = ['pdf-one', 'gif-two', 'jpg-three'] 
>>> list(chain.from_iterable([[a, b+c] for a, b, c 
              in (x.partition('-') for x in images)])) 
['pdf', '-one', 'gif', '-two', 'jpg', '-three'] 

使用发电机功能进行更多可读溶液:

def my_split(seq): 
    for item in seq: 
     a, b, c = item.partition('-') 
     yield a 
     yield b+c 

>>> list(my_split(images)) 
['pdf', '-one', 'gif', '-two', 'jpg', '-three'] 
相关问题