![](/img/trans.png)
[英]How to combine two lists into a dictionary with the values to the keys assigned manually
[英]How to Combine Two Lists into Keys and Values and Add to Dictionary?
我想將 list_A 變成鍵,將 list_B 變成值。 然后我想將這些列表附加到字典 dict_A 中。
舉些例子,
list_A = ['age', 'weight', 'height']
list_B = [21, 205, 72]
dict_A = {'name': 'hank'}
所需的 output:
{'name': 'hank', 'age': 21, 'weight': 205, 'height': 72}
提前感謝您提供的任何幫助!
要通過配對keys
列表和values
列表來創建dict
,您可以使用zip
dict(zip(list_A, list_B))
然后用它來更新dict_A
list_A = ['age', 'weight', 'height']
list_B = [21, 205, 72]
dict_A = {'name': 'hank'}
dict_A.update(dict(zip(list_A, list_B)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.