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(
Solution 1:
You need to use list.extend
instead of list.append
.
newlist = []
for word inlines:
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']
Solution 2:
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 indatafor item in items.split(",")]
['60', '78', '70', '77', '80', '74', '90', '75', '100', '74', '110', '75']
Solution 3:
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
Solution 4:
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)
Post a Comment for "Split A List Of Strings By Comma"