2016-04-26 100 views
0

我正在做一些与文件数据有关的东西,并且我已经将其每个列与其信息压缩了,但是现在我想将来自其他文件的信息(其中我已经压缩了信息)和我不知道如何解压缩并将其融合在一起。元组的列表清单

编辑: 我有几个拉链对象:

l1 = [('a', 'b'), ('c', 'd')] # list(zippedl1) 
l2 = [('e', 'f'), ('g', 'h')] # list(zippedl1) 
l3 = [('i', 'j'), ('k', 'm')] # list(zippedl1) 

,我想解压,如:

unzipped = [('a', 'c', 'e', 'g', 'i', 'k'), ('b', 'd', 'f', 'h', 'j', 'm')] 

我不希望压缩的结构变换到一个列表,只是为了记忆的原因。我搜索了,我没有找到可以帮助我的东西。希望你能帮助我! [对不起我的英文不好]

+0

'zip'对象使用_less_内存而不是'list',因为它们被懒惰地评估。这是假设你的“zip对象”确实是'zip'对象,因为它们看起来像我的'list'文字。 – TigerhawkT3

回答

0

我相信你要压缩的解压chain

# Leaving these as zip objects as per your edit 
l1 = zip(('a', 'c'), ('b', 'd')) 
l2 = zip(('e', 'g'), ('f', 'h')) 
l3 = zip(('i', 'k'), ('j', 'm')) 

unzipped = [('a', 'c', 'e', 'g', 'i', 'k'), ('b', 'd', 'f', 'h', 'j', 'm')] 

你可以简单地做

from itertools import chain 
result = list(zip(*chain(l1, l2, l3))) 

# You can also skip list creation if all you need to do is iterate over result: 
# for x in zip(chain(l1, l2, l3)): 
#  print(x) 

print(result) 
print(result == unzipped) 

此打印:

[('a', 'c', 'e', 'g', 'i', 'k'), ('b', 'd', 'f', 'h', 'j', 'm')] 
True 
+0

It works!Thank you,many love <3 – Zealot

0

您需要先串联列表:

>>> l1 = [('a', 'b'), ('c', 'd')] 
>>> l2 = [('e', 'f'), ('g', 'h')] 
>>> l3 = [('i', 'j'), ('k', 'm')] 
>>> zip(*(l1 + l2 + l3)) 
[('a', 'c', 'e', 'g', 'i', 'k'), ('b', 'd', 'f', 'h', 'j', 'm')] 
+0

我错过了一些关于解释的东西,对不起:(我有l1,l2和l3作为zip对象,我不想将它们放入列表中! – Zealot