英文:
Creating a dictionary from 2 lists with same keys
问题
以下是翻译好的部分:
"as i m beginner in python." -> "由于我是Python的初学者。"
"I have below 2 lists:" -> "我有以下两个列表:"
"list1 =['abc','cbd','efg']" -> "list1 = ['abc', 'cbd', 'efg']"
"list2['applicationName','appllicationName','appllicationName']" -> "list2['applicationName', 'applicationName', 'applicationName']"
"output i am expected like below:" -> "我期望的输出如下:"
"[{'appllicationName':'abc'},{'appllicationName':'cbd'},{'appllicationName':'efg'}]" -> "[{'applicationName':'abc'},{'applicationName':'cbd'},{'applicationName':'efg'}]"
"using dict,zip function i am getting only below output:" -> "使用dict和zip函数,我只得到了以下输出:"
"{'appllicationName':'efg'}" -> "{'applicationName':'efg'}"
"please help me resolve this." -> "请帮助我解决这个问题。"
英文:
as i m beginner in python.
I have below 2 lists:
list1 =['abc','cbd','efg']
list2['applicationName','appllicationName','appllicationName']
output i am expected like below:
[{'appllicationName':'abc'},{'appllicationName':'cbd'},{'appllicationName':'efg'}]
using dict,zip function i am getting only below output:
{'appllicationName':'efg'}
please help me resolve this.
答案1
得分: 1
I'm not sure about what you want. But I assume you need a list of 3 single dictionaries. But @Manuel's answer with one dictionary seems better to me.
Anyway... Sticking to your example code you can do it like this.
#!/usr/bin/env python3
list1 = ['abc', 'cbd', 'efg']
list2 = ['applicationName', 'appllicationName', 'appllicationName']
# Method 1
result_one = [{key: val} for key, val in zip(list2, list1)]
# Method 2 assumes that the key name is always the same.
key = 'applicationName'
result_two = [{key: val} for val in list1]
Both methods give you
[{'applicationName': 'abc'}, {'applicationName': 'cbd'}, {'applicationName': 'efg'}]
英文:
I'm not sure about what you want. But I assume you need a list of 3 single dictionaries. But @Manuel 's answer with one dictionary seems better to me.
Anyway... Sticking to your example code you can do it like this.
#!/usr/bin/env python3
list1 = ['abc', 'cbd', 'efg']
list2 = ['applicationName', 'appllicationName', 'appllicationName']
# Method 1
result_one = [{key: val} for key, val in zip(list2, list1)]
# Method 2 assumes that the key name is always the same.
key = 'applicationName'
result_two = [{key: val} for val in list1]
Both methods give you
[{'applicationName': 'abc'}, {'applicationName': 'cbd'}, {'applicationName': 'efg'}]
答案2
得分: 0
你不能在Python字典中有相同的键两次,因此它只保留最后一个键值对,因为你总是替换它们。你可以像这样将值添加到同一个键中:
list1 = ['abc', 'cbd', 'efg']
list2 = ['appllicationName', 'appllicationName', 'appllicationName']
my_dict = {}
for key, value in zip(list2, list1):
if key not in my_dict:
my_dict[key] = []
my_dict[key].append(value)
print(my_dict)
这里的输出将是:
{'appllicationName': ['abc', 'cbd', 'efg']}
英文:
You cant have the same key twice in a Python Dictionary, so he only takes the last key:value since you always replace them. You could add the Values to the same Key like this:
list1 =['abc','cbd','efg']
list2 = ['appllicationName','appllicationName','appllicationName']
my_dict = {}
for key, value in zip(list2, list1):
if key not in my_dict:
my_dict[key] = []
my_dict[key].append(value)
print(my_dict)
Here the output will be:
{'appllicationName': ['abc', 'cbd', 'efg']}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论