是否有映射迭代器的每个元素的函数。序号到另一个相应的?
Is there a function for mapping each elem of an iter. seq. to corresponding ones of another?
我知道 dict.fromkeys(a, b)
采用可迭代的键序列 (a
),但只采用一个(可选)值(默认为 None
)。我也知道 update({k1: v1, k2: v2...})
接受字典,或者类似 (key, value)
元组列表的东西。但我一直无法找到看起来像这样的方法:-
new_dict = dict.method(lst1, lst2)
将 lst1
的每个元素映射到 lst2
.
的相应元素
我知道我可以使用 map()
函数,例如,将 lambda
作为参数或类似的东西,但我一直在寻找直接(更有效?)的替代品.
谢谢!
使用zip
将列表压缩成(key, value)
个元组,然后将结果传递给dict()
。
>>> lst1 = ['foo', 'bar']
>>> lst2 = [42, True]
>>> dict(zip(lst1, lst2))
{'foo': 42, 'bar': True}
我知道 dict.fromkeys(a, b)
采用可迭代的键序列 (a
),但只采用一个(可选)值(默认为 None
)。我也知道 update({k1: v1, k2: v2...})
接受字典,或者类似 (key, value)
元组列表的东西。但我一直无法找到看起来像这样的方法:-
new_dict = dict.method(lst1, lst2)
将 lst1
的每个元素映射到 lst2
.
我知道我可以使用 map()
函数,例如,将 lambda
作为参数或类似的东西,但我一直在寻找直接(更有效?)的替代品.
谢谢!
使用zip
将列表压缩成(key, value)
个元组,然后将结果传递给dict()
。
>>> lst1 = ['foo', 'bar']
>>> lst2 = [42, True]
>>> dict(zip(lst1, lst2))
{'foo': 42, 'bar': True}