Python lists and their splitting

For example, I have code like this

a = ["a;b", "c;d",...,"y;z"] 

I want to break each list item into elements of the same list. So I want to get something like this:

 ["a", "b", "c", "d", ...., "y", "z"] 

How can i do this? Thank you for your responses.

+4
source share
9 answers

Using only string operations seems simplest (this is, of course, subjective) and faster (with a huge margin compared to other solutions published so far).

 >>> a = ["a;b", "c;d", "y;z"] >>> ";".join(a).split(";") ['a', 'b', 'c', 'd', 'y', 'z'] 

Evidence / Benchmarks

Sorted in ascending order of elapsed time:

 python -mtimeit -s'a=["a;b","x;y","p;q"]*99' '";".join(a).split(";")' 10000 loops, best of 3: 48.2 usec per loop python -mtimeit -s'a=["a;b","x;y","p;q"]*99' '[single for pair in a for single in pair.split(";")]' 1000 loops, best of 3: 347 usec per loop python -mtimeit -s'from itertools import chain; a=["a;b","x;y","p;q"]*99' 'list(chain(*(s.split(";") for s in a)))' 1000 loops, best of 3: 350 usec per loop python -mtimeit -s'a=["a;b","x;y","p;q"]*99' 'sum([x.split(";") for x in a],[])' 1000 loops, best of 3: 1.13 msec per loop python -mtimeit -s'a=["a;b","x;y","p;q"]*99' 'sum(map(lambda x: x.split(";"), a), [])' 1000 loops, best of 3: 1.22 msec per loop python -mtimeit -s'a=["a;b","x;y","p;q"]*99' 'reduce(lambda x,y:x+y, [pair.split(";") for pair in a])' 1000 loops, best of 3: 1.24 msec per loop 
+9
source

You can use itertools.chain :

 >>> a = ["a;b", "c;d","y;z"] >>> list(itertools.chain(*(s.split(';') for s in a))) ['a', 'b', 'c', 'd', 'y', 'z'] 
+5
source

A slightly more functional approach:

 >>> l = ["a;b", "c;d", "e;f", "y;z"] >>> sum(map(lambda x: x.split(';'), l), []) ['a', 'b', 'c', 'd', 'e', 'f', 'y', 'z'] 
+3
source

This job:

 l = [] for item in ["a;b", "c;d", "e;f"]: l += item.split(";") print l 

He gives:

 ['a', 'b', 'c', 'd', 'e', 'f'] 
+1
source
 a = ["a;b", "c;d","y;z"] print [atom for pair in a for atom in pair.split(';')] 

gives what you want:

 ['a', 'b', 'c', 'd', 'y', 'z'] 

note: I cannot tell you how to get from "..." to "...." in the middle of your array :)

+1
source
 l = [] for current in [c.split(';') for c in a]: l.extend(current) 

You might want to read the help on the lists http://docs.python.org/tutorial/datastructures.html#list-comprehensions

0
source
 a = ["a;b", "c;d","e;f","y;z"] b = [] for i in a: c = i.split(';') b = b + c print b 
0
source

Longer than Felix Kling, but here. First divide the list into sub-lists

 >>> a_split = [i.split(";", 1) for i in a] 

This will result in a list of the form:

 [[a,b], [c,d], ..., [y,z]] 

Now you need to "combine" the internal and external lists. The built-in reduce() function is ideal for this:

 >>> reduce(lambda x, y: x + y, a_split) 

Voila:

 ['a', 'b', 'c', 'd', ... 'y', 'z'] 
0
source

You can use the lines for this:

 >>> a = ["a;b", "c;d","y;z"] >>> list(''.join(a).replace(';', '')) ['a', 'b', 'c', 'd', 'y', 'z'] 

This solution is one of the fastest proposed so far:

 # Shawn Chin solution (the fastest so far, by far): python -mtimeit -s'a=["a;b","x;y","p;q"]*99' '";".join(a).split(";")' 10000 loops, best of 3: 27.4 usec per loop # This solution: python -mtimeit -s'a=["a;b","x;y","p;q"]*99' "list(''.join(a).replace(';', ''))" 10000 loops, best of 3: 33.5 usec per loop 

The moral is that lists represented by strings can be quite efficient in this case, possibly due to simpler memory handling (characters are stored in sequential memory cells).

0
source

Source: https://habr.com/ru/post/1339347/


All Articles