>>> 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]