简体   繁体   中英

Split a list of strings by comma

I want to convert

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

in to

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

I thought I could use

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

but this produces this instead:

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

Can anyone please offer a solution?

You need to use list.extend instead of list.append .

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

Or, using 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 actually returns a list.

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

Since you are appending the returned list to newlist , you are getting a list of lists. Instead use list.extend method, like this

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

But you can simply use nested list comprehension like this

>>> 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']

using 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']

The more items you have to flatten chain does it a bit more efficiently:

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

I think this was the easiest way (thanks to a friend who helped with this)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM