简体   繁体   English

Python列表及其拆分

[英]Python lists and their splitting

For example, I have such code 例如,我有这样的代码

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

I want to split every list element into to items of the same list. 我想将每个列表元素拆分为同一列表中的项目。 So i wanna get something like this: 所以我想得到这样的东西:

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

How can I do such thing? 我该怎么办? Thanks for your answers. 谢谢你的回答。

Using only string operations seem to be simplest (this is subjective, of course) and fastest (by a huge margin, compared to other solutions posted so far). 仅使用字符串操作似乎是最简单的 (当然这是主观的)和最快的 (与迄今为止发布的其他解决方案相比,差距很大)。

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

Proof / 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

You can use itertools.chain : 你可以使用itertools.chain

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

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

That's work : 这是工作:

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

print l

It gives : 它给 :

['a', 'b', 'c', 'd', 'e', 'f']
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 can't tell you how to get from '...' to '....' in the middle of your array :) 注意:我无法告诉你如何在阵列中间从'...'到'....'

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

You might want to read up on list comprehensions http://docs.python.org/tutorial/datastructures.html#list-comprehensions 您可能想要阅读列表推导http://docs.python.org/tutorial/datastructures.html#list-comprehensions

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

print b

A bit longer than Felix Kling's answer, but here goes. 比费利克斯克林的回答要长一点,但这里有。 First split 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]]

You now need to 'merge' the inner and outer lists in some way. 您现在需要以某种方式“合并”内部和外部列表。 The builtin reduce() function is a perfect fit for this: 内置的reduce()函数非常适合这个:

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

Voila: 瞧:

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

Strings can be used 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 suggested so far: 此解决方案是目前为止最快的解决方案之一:

# Shawn Chin's 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 morale is that lists represented by strings can be quite efficient in this case, possibly because of a simpler memory handling (characters are stored in consecutive memory locations). 士气是在这种情况下由字符串表示的列表可能非常有效,可能是因为更简单的内存处理(字符存储在连续的内存位置)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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