raylee2015 发表于2年前

• 发表于 2年前
• 阅读 1
• 收藏 0
• 评论 0

``````>>> list(zip([1,2,3],[4,5,6,7]))
[(1, 4), (2, 5), (3, 6)]
>>> list(zip('abcdef','gh'))
[('a', 'g'), ('b', 'h')]
>>> list(zip([1,2,3]))
[(1,), (2,), (3,)]
>>> list(map(abs,[-2,-1,0,1,2]))
[2, 1, 0, 1, 2]
>>> list(map(pow,[1,2,3],[4,5,6,7,8]))
[1, 32, 729]
>>>``````

1.zip对序列的配对操作

2.map的一般使用

3.map里面的嵌套操作，有着zip的参与

``````>>> list(map(pow,[1,2,3],[4,5,6,7,8]))
[1, 32, 729]
>>>``````

``````>>> def test(func,*args):
res=[]
for item in zip(*args):
res.append(func(*item))
return res

>>> test(pow,[1,2,3],[4,5,6,7,8])
[1, 32, 729]
>>>``````

``````>>> def test(func,*args):
return [func(*item) for item in zip(*args)]

>>> test(pow,[1,2,3],[4,5,6,7,8])
[1, 32, 729]
>>>``````

``````>>> def test(func,*args):
return (func(*item) for item in zip(*args))

>>> test(pow,[1,2,3],[4,5,6,7,8])
<generator object <genexpr> at 0x0240C030>
>>> list(test(pow,[1,2,3],[4,5,6,7,8]))
[1, 32, 729]
>>>``````

``````>>> def test(func,*args):
for item in zip(*args):
yield func(*item)

>>> test(pow,[1,2,3],[4,5,6,7,8])
<generator object test at 0x02404FD0>
>>> list(test(pow,[1,2,3],[4,5,6,7,8]))
[1, 32, 729]
>>>``````

------------------------------------------------------------------

×