Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python2.7中从元组列表中解压项_Python_Python 2.7_List - Fatal编程技术网

在Python2.7中从元组列表中解压项

在Python2.7中从元组列表中解压项,python,python-2.7,list,Python,Python 2.7,List,我有3个列表,我正在使用izip以特定的方式组合它们。然后,我对元组的组合列表执行计算。但是,一旦计算完成,我需要“撤消”此操作以恢复原始列表,尽管有些值已更改。有没有一种简单的蟒蛇式的方法 grouped = [] med_done = 0 lrg_done = 0 #While items exist in small, pair up one item from each list grouped.extend(list(izip(sml,med,lrg))) don

我有3个列表,我正在使用izip以特定的方式组合它们。然后,我对元组的组合列表执行计算。但是,一旦计算完成,我需要“撤消”此操作以恢复原始列表,尽管有些值已更改。有没有一种简单的蟒蛇式的方法

grouped = []

med_done = 0
lrg_done = 0        

#While items exist in small, pair up one item from each list
grouped.extend(list(izip(sml,med,lrg)))

done = len(sml)

# While items remain in med, pair up one from med an two from lrg
grouped.extend(list(izip(med[done:],lrg[done::2],lrg[(done+1)::2])))

done = done + (2*(len(med)-len(sml)))

# Finish pairing up remaining items from lrg
grouped.extend(list(izip_longest(lrg[done::3],lrg[(done+1)::3],lrg[(done+2)::3],fillvalue = '-')))    
这将产生:

smlBinary:[6 6 7 7 7 8 6 8 7]

medBinary:[4 3 4 3 3 3 5 5 4 5 3 5 3 3 4 4]

lrgBinary:[0 1 2 0 0 2 1 1 1 1 1 2 2 2 0 2 2 1 2 1 2 1 0 1 2 1 0 2 1 2 0 1 0 1 0 1]

Grouped[(6, 4, 0), (6, 3, 1), (7, 4, 2), (7, 3, 0), (7, 3, 0), (8, 3, 2), (6, 5, 1), (8, 5, 1), (7, 4, 1), (5, 0, 1), (3, 2, 0), (5, 0, 2), (3, 1, 1), (3, 1, 1), (4, 1, 2), (4, 2, 2), (1, 2, 1), (0, 2, 1), (2, 0, 1), (0, 1, 0), (1, '-', '-')]
问题:
现在有没有一种类似的技术可以用来将这些项目“解绑”回原始列表

如果我理解正确,你只需要将元组拆分成列表?我对izip了解不多,但看起来您只需了解一些列表即可:

sm_out = [tup[0] for tup in grouped]
med_out = [tup[1] for tup in grouped]
lg_out = [tup[2] for tup in grouped]

您可以使用变量参数和zip函数的组合来解压

unzipped = zip(*zipped)

其中zipped是您的分组变量。

Hi@EyuelDK,是的,我希望使用类似的东西,但我需要使用3个“片段”,与izip部分相同,但不使用循环。我不理解您的评论,但在您的情况下,“unzipped”应该成为一个长度为3的数组,其中每个数组都是原始的,即sml、med、,lrg.Hi@Anddrrw,如果它是均匀配对的,那么你的方法将非常有效,但是配对的方式意味着我不能对整个“分组”列表使用单一方法。至少要分三部分来完成。