Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有两个列表,例如:
L = [1, 2] S = ['B', 'C']
我怎样才能将它们组合成这样的字典:
X = {'B': 1, 'C': 2}
列表将始终具有相同的长度,但可以包含任意数量的项目。
这是一个单行:
dict(zip(S, L))
这边走:
>>> key_list = ['a', 'b'] >>> value_list = [1, 2] >>> result = dict(zip(key_list, value_list)) >>> print result {'a': 1, 'b': 2} >>> _