简体   繁体   English

使用相同键但值不同的Python字典

[英]Python dictionaries using same keys but different values

I want to create a dictionary from key and value pairs. 我想从键和值对创建一个字典。 The problem is that I have same keys but different values. 问题是我有相同的键但值不同。 So my goal is to create 所以我的目标是创造

menu = [
    {"viewclass": "MDMenuItem",
     "text" : "option1"},
    {"viewclass": "MDMenuItem",
     "text" : "option2"}
]

I tried the creation of this variable by using a for loop 我尝试使用for循环创建此变量

length = 2
menu = {}

view_class_keys = length * ["viewclass"]
view_class_values = length * ["MDMenuItem"]
text_keys = length * ["text"]
text_values = ["option1", "option2"]

for iterator in range(0, length):
    menu[view_class_keys[iterator]] = view_class_values[iterator]
    menu[text_keys[iterator]] = text_values[iterator]

print([menu])

# Output: [{'viewclass': 'MDMenuItem', 'text': 'option2'}]

I know the problem is that the keys are the same, but I do not know how to resolve this problem. 我知道问题在于密钥相同,但是我不知道如何解决此问题。

You are quite close. 你很亲密 You should aggregate your dictionaries in the list while creating a new dictionary on each iteration appending it to the resulting list: 您应该在列表中汇总字典,同时在每次迭代中创建新字典,然后将其追加到结果列表中:

length = 2
menu_list = []
view_class_keys = length * ["viewclass"]
view_class_values = length * ["MDMenuItem"]
text_keys = length * ["text"]
text_values = ["option1", "option2"]

for iterator in range(0, length):
    menu = {}
    menu[view_class_keys[iterator]] = view_class_values[iterator]
    menu[text_keys[iterator]] = text_values[iterator]
    menu_list.append(menu)

print(menu_list)

Edit: Assuming the only variable part in your code is text_values list, your code can be simplified to 编辑:假设您代码中唯一的可变部分是text_values列表,则您的代码可以简化为

menu = [{"viewclass": "MDMenuItem", "text" : option} for option in text_values]

也许是这样的:

menu = [dict(zip(i[::2], i[1::2])) for i in zip(view_class_keys, view_class_values, text_keys, text_values)]

OR: 要么:

length = 2
text_values = ["option1", "option2"]
print([dict(viewclass="MDMenuItem", text=option) for option in text_values])

This should resolve your problem: 这应该可以解决您的问题:

length = 2
# in your goals, "menu" type is a list
menu = []

view_class_keys = length * ["viewclass"]
view_class_values = length * ["MDMenuItem"]
text_keys = length * ["text"]
text_values = ["option1", "option2"]


for iterator in range(0, length):
    #first time you have to append to the list a new dictionary
    menu.append({view_class_keys[iterator]:view_class_values[iterator]})
    #than you can add a new key value to the dict 
    menu[iterator][text_keys[iterator]] = text_values[iterator]

#I leave the square brackets cause menu is already a list
print(menu)

# Output: [{'viewclass': 'MDMenuItem', 'text': 'option1'}, {'viewclass': 'MDMenuItem', 'text': 'option2'}]

However the dict may not be the best choise for you since you have same keys for different values. 但是,由于您为不同的值使用相同的键,因此dict可能不是您的最佳选择。 Maybe you can try with a single list like this: 也许您可以尝试使用以下单个列表:

length = 2
menu = []

view_class_keys = length * ["viewclass"]
view_class_values = length * ["MDMenuItem"]
text_keys = length * ["text"]
text_values = ["option1", "option2"]

for iterator in range(0, length):
    menu.append([view_class_keys[iterator],view_class_values[iterator]])
    menu.append([text_keys[iterator],text_values[iterator]])

print(menu)
# Output: [['viewclass', 'MDMenuItem'], ['text', 'option1'], ['viewclass', 'MDMenuItem'], ['text', 'option2']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM