用逗号分割字符串列表

Split a list of strings by comma

我要转换

['60,78', '70,77', '80,74', '90,75', '100,74', '110,75']

['60', '78', '70', '77'.. etc]

我以为我可以使用

for word in lines:
    word = word.split(",")
    newlist.append(word)
return newlist

但这会产生这个:

[['60', '78'], ['70', '77'], ['80', '74'], ['90', '75'], ['100', '74'], ['110', '75']]

谁能提供解决方案?

您需要使用 list.extend 而不是 list.append

newlist = []
for word in lines:
    word = word.split(",")
    newlist.extend(word)  # <----
return newlist

或者,使用 list comprehension:

>>> lst = ['60,78', '70,77', '80,74', '90,75', '100,74', '110,75']
>>> [x for xs in lst for x in xs.split(',')]
['60', '78', '70', '77', '80', '74', '90', '75', '100', '74', '110', '75']

str.split 实际上 returns 一个列表。

Return a list of the words in the string, using sep as the delimiter string.

由于您将返回的列表附加到 newlist,因此您将获得一个列表列表。而是使用 list.extend 方法,像这样

for word in lines:
    newlist.extend(word.split(","))

但是你可以像这样简单地使用嵌套列表理解

>>> data = ['60,78', '70,77', '80,74', '90,75', '100,74', '110,75']
>>> [item for items in data for item in items.split(",")]
['60', '78', '70', '77', '80', '74', '90', '75', '100', '74', '110', '75']

使用 itertools.chain :

from itertools import chain

print(list(chain.from_iterable(ele.split(",") for ele in l)))
['60', '78', '70', '77', '80', '74', '90', '75', '100', '74', '110', '75']

您需要展平链条的项目越多,效率就越高:

In [1]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20" for _ in range(100000)]

In [2]: from itertools import chain

In [3]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30" for _ in range(10000)]

In [4]: timeit (list(chain.from_iterable(ele.split(",") for ele in l)))
100 loops, best of 3: 17.7 ms per loop

In [5]: timeit  [item for items in l for item in items.split(",")]
10 loops, best of 3: 20.9 ms per loop

我认为这是最简单的方法(感谢一位朋友的帮助)

list=['60,78', '70,77', '80,74', '90,75', '100,74', '110,75']
for word in list:
    chapter, number = word.split(',') #word = word.split(',')
    print(word)