python zip函数

这篇写的很清楚:

一、学会help

  首先可以使用help(zip)了解一下zip

  

>>> help(zip)
Help on built-in function zip in module __builtin__:

zip(...)
    zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]

    Return a list of tuples, where each tuple contains the i-th element
    from each of the argument sequences.  The returned list is truncated
    in length to the length of the shortest argument sequence.

 

二、高级功能

1、使用zip反转字典
>>> m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> m.items()
[('a', 1), ('c', 3), ('b', 2), ('d', 4)]
>>> zip(m.values(), m.keys())
[(1, 'a'), (3, 'c'), (2, 'b'), (4, 'd')]
>>> mi = dict(zip(m.values(), m.keys()))
>>> mi
{1: 'a', 2: 'b', 3: 'c', 4: 'd'}

 

 

 

 

 

 

 

 

----------------------2016-8-1 13:49:24--

  source:【1】python 强大的zip

      【2】python zip函数

posted @ 2016-07-30 08:23  半天的半天  阅读(1351)  评论(0编辑  收藏  举报