Python - 多个迭代的排气图

Pho*_*nix 2 python iterable map python-3.x

a=['1','2','3']
Run Code Online (Sandbox Code Playgroud)

map(int,a) 可以使用list来耗尽输出:

list(map(int,a))
Run Code Online (Sandbox Code Playgroud)

输出: 1,2,3


a=['1','2','3']
b=['1','2']
Run Code Online (Sandbox Code Playgroud)

如何耗尽以下条件:

map(int,a,b)
Run Code Online (Sandbox Code Playgroud)

the*_*eye 5

如果要将每个列表转换为整数列表,则可以执行此操作

a, b = ['1','2','3'], ['1','2']
print(list(map(lambda x: map(int,x), [a, b])))
# [[1, 2, 3], [1, 2]]
Run Code Online (Sandbox Code Playgroud)

这可以分配给ab返回

a, b = map(lambda x: map(int,x), [a, b])
Run Code Online (Sandbox Code Playgroud)

如果你想链接元素,你可以itertools.chain像这样使用

from itertools import chain
print(list(map(int, chain(a,b))))
# [1, 2, 3, 1, 2]
Run Code Online (Sandbox Code Playgroud)

编辑:如果你想传递多于iterable作为参数,那么该函数也必须接受许多参数.例如,

a, b = [1, 2, 3], [1, 2, 3]
print(list(map(lambda x, y: x + y, a, b)))
# [2, 4, 6]
Run Code Online (Sandbox Code Playgroud)

如果我们传递三个迭代,函数必须接受三个参数,

a, b, c = [1, 2, 3], [1, 2, 3], [1, 2, 3]
print(list(map(lambda x, y, z: x + y + z, a, b, c)))
# [3, 6, 9]
Run Code Online (Sandbox Code Playgroud)

如果可迭代的大小不同,则将考虑最小大小的可迭代的长度.所以

a, b, c = [1, 2, 3], [1, 2, 3], [1, 2]
print(list(map(lambda x, y, z: x + y + z, a, b, c)))
# [3, 6]
Run Code Online (Sandbox Code Playgroud)