python中zip的反函数是什么? [英] What is the inverse function of zip in python?
问题描述
Possible Duplicate:
A Transpose/Unzip Function in Python
我已经使用了numpy库中的zip()函数对元组进行排序,现在我有了一个包含所有元组的列表.从那以后,我修改了该列表,现在我想还原元组,以便可以使用我的数据.我该怎么办?
I've used the zip() function from the numpy library to sort tuples and now I have a list containing all the tuples. I had since modified that list and now I would like to restore the tuples so I can use my data. How can I do this?
推荐答案
lst1, lst2 = zip(*zipped_list)
应该给您解压缩的列表.
should give you the unzipped list.
*zipped_list
解压缩zipped_list对象.然后,它将所有元组从zipped_list对象传递到zip,zip将它们打包回去时再打包.
*zipped_list
unpacks the zipped_list object. it then passes all the tuples from the zipped_list object to zip, which just packs them back up as they were when you passed them in.
所以,如果:
a = [1,2,3]
b = [4,5,6]
然后zipped_list = zip(a,b)
给您:
[(1,4), (2,5), (3,6)]
和*zipped_list
可以给您
(1,4), (2,5), (3,6)
使用zip(*zipped_list)
压缩后,会返回两个集合:
zipping that with zip(*zipped_list)
gives you back the two collections:
[(1, 2, 3), (4, 5, 6)]
这篇关于python中zip的反函数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!