Zip lists in Python

前端 未结 10 2094
无人及你
无人及你 2020-11-22 02:09

I am trying to learn how to \"zip\" lists. To this end, I have a program, where at a particular point, I do the following:

x1, x2, x3 = stuff.calculations(wi         


        
10条回答
  •  悲哀的现实
    2020-11-22 02:37

    Basically the zip function works on lists, tuples and dictionaries in Python. If you are using IPython then just type zip? And check what zip() is about.

    If you are not using IPython then just install it: "pip install ipython"

    For lists

    a = ['a', 'b', 'c']
    b = ['p', 'q', 'r']
    zip(a, b)
    

    The output is [('a', 'p'), ('b', 'q'), ('c', 'r')

    For dictionary:

    c = {'gaurav':'waghs', 'nilesh':'kashid', 'ramesh':'sawant', 'anu':'raje'}
    d = {'amit':'wagh', 'swapnil':'dalavi', 'anish':'mane', 'raghu':'rokda'}
    zip(c, d)
    

    The output is:

    [('gaurav', 'amit'),
     ('nilesh', 'swapnil'),
     ('ramesh', 'anish'),
     ('anu', 'raghu')]
    

提交回复
热议问题