Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将两个列表合并为键和值并添加到字典中?_Python_List_Dictionary - Fatal编程技术网

Python 如何将两个列表合并为键和值并添加到字典中?

Python 如何将两个列表合并为键和值并添加到字典中?,python,list,dictionary,Python,List,Dictionary,我想把列表A转换成键,把列表B转换成值。然后我想把这些清单附在字典上,dict_A 比如, list_A = ['age', 'weight', 'height'] list_B = [21, 205, 72] dict_A = {'name': 'hank'} 期望输出: {'name': 'hank', 'age': 21, 'weight': 205, 'height': 72} 提前感谢您提供的任何帮助 要通过配对键列表和值列表来创建dict,您可以使用zip dict(zip

我想把列表A转换成键,把列表B转换成值。然后我想把这些清单附在字典上,dict_A

比如,

list_A = ['age', 'weight', 'height']

list_B = [21, 205, 72]

dict_A = {'name': 'hank'}
期望输出:

{'name': 'hank', 'age': 21, 'weight': 205, 'height': 72}


提前感谢您提供的任何帮助

要通过配对
键列表
值列表
来创建
dict
,您可以使用
zip

dict(zip(list_A, list_B))
然后用它来更新
dict\u A

list_A = ['age', 'weight', 'height']
list_B = [21, 205, 72]
dict_A = {'name': 'hank'}
dict_A.update(dict(zip(list_A, list_B)))

谢谢@azro为您抽出时间!非常感谢先生=)这段代码工作得非常好!