zip()函数将对象中对应的元素打包成一个元组,返回由这些元组组成的列表
In [30]: g=[1,2,3]
In [31]: h=[6,7,8]
In [32]: l=zip(g,h)
In [33]: l
Out[33]: <zip at 0x7fb9a90c9540>
In [34]: o=list(l)
In [35]: o
Out[35]: [(1, 6), (2, 7), (3, 8)]
利用 * 号操作符,可以将元组解压为列表
operator.itemgetter(1)按字典的值进行排序,reverse=True为降序
In [23]: a={'a':1,'b':2,'c':3}
In [24]: a
Out[24]: {'a': 1, 'b': 2, 'c': 3}
In [25]: b=sorted(a.items(),key=operator.itemgetter(1),reverse=True)
In [26]: b
Out[26]: [('c', 3), ('b', 2), ('a', 1)]
In [27]:
In [27]: c=zip(*b)
In [28]: d=list(c)
In [29]: d
Out[29]: [('c', 'b', 'a'), (3, 2, 1)]
In [36]: c1,c2=zip(*b)
In [37]: c1
Out[37]: ('c', 'b', 'a')
In [38]: c2
Out[38]: (3, 2, 1)