zoukankan      html  css  js  c++  java
  • python基础===map和zip的用法

    >>> list1=[1,45,232,45,666,64]
    >>> list2=["ss","kein","tom","sda","qq","da"]
    >>> d=dict(zip(list1,list2))
    >>> print(d)
    {1: 'ss', 45: 'sda', 232: 'tom', 666: 'qq', 64: 'da'}
    

    zip,实现把两个list组合成一个dict

    格式为: dict(zip(keys,vals))

     

    >>> L1=[1,2,3,4]
    >>> L2=[5,6,7,8]
    >>> zipped=zip(L1,L2)
    >>> print(zipped)
    <zip object at 0x02C5B238>
    >>> print(list(zipped))
    [(1, 5), (2, 6), (3, 7), (4, 8)]
    >>> print(dict(zipped))
    {}
    >>> print(dict(zip(L1,L2)))
    {1: 5, 2: 6, 3: 7, 4: 8}
    >>> print(list(zip(L1,L2)))
    [(1, 5), (2, 6), (3, 7), (4, 8)]
    

     

     再举例map的用法:

    >>> l=[1,3,5,7,9,10]
    >>> def add100(x):
    	return x+100
    
    >>> map(add100,l)
    <map object at 0x02B7A750>
    >>> print(list(map(add100,l)))
    [101, 103, 105, 107, 109, 110]
    

    顺便提一下


    如果你也喜欢Python 这里有一群Python爱好者汇集在此。

    关注微信公众号:【软件测试技术】,回复 888,获取QQ群号。 

     

  • 相关阅读:
    2.8
    2.7
    2.6
    2.5
    2.4第三篇读后感
    2.2第一篇读后感
    2.1
    字符统计
    6468: Snuke's Coloring
    6463: Tak and Hotels II
  • 原文地址:https://www.cnblogs.com/botoo/p/7380154.html
Copyright © 2011-2022 走看看