lambda arguments unpack error

后端 未结 6 1851
名媛妹妹
名媛妹妹 2020-11-30 09:40

In Python 2 this code is OK:

f = lambda (m, k): m + k

m = [1,2,3,4]
k = [5,6,7,8]

print(map(f, zip(m, k)))

but in Python 3 the following

6条回答
  •  自闭症患者
    2020-11-30 09:45

    You can use the same syntax in both Python 2 and Python 3 if you use itertools.starmap instead of map which unpacks the tuple items for us:

    >>> from itertools import starmap
    >>> f = lambda m, k: m + k
    >>> list(starmap(f, zip(m, k)))
    [6, 8, 10, 12]
    

提交回复
热议问题