Python - 如果为真,则将变量分配为一个字典或另一个字典
Python - Assign variable as one dictionary or another if something is true
如果我有两个字典,并且我想根据外部输入将其分配给另一个变量,是否有更 Pythonic 的方式来完成它。
dict_one = {"id": 1, "content": "content of the first dict"}
dict_two = {"id": 2, "content": "content of the second dict"}
dict_three = {"id": 3, "content": "content of the third dict"}
#insert many more dicts....
outside_input = 1
if outside_input == 1:
result = dict_one
elif outside_input == 3:
result = dict_three
如果没有关于您的问题的更多详细信息,我可能会使用嵌套字典,例如:
dict_of_dicts = {
'dict_one': {"id": 1, "content": "content of the first dict"},
'dict_two': {"id": 2, "content": "content of the second dict"}
}
outside_input = 'dict_one'
result = dict_of_dicts[outside_input]
或者,如果字典中的 id 恰好存在,出于这个原因,您可以将其拉出作为键以减少冗余:
dict_of_dicts = {
1: {"content": "content of the first dict"},
2: {"content": "content of the second dict"}
}
还是第三种方式,但在搜索特定词典时速度较慢
list_of_dicts = [
{"id": 1, "content": "content of the first dict"},
{"id": 2, "content": "content of the second dict"}
]
outside_input = 'dict_one'
result = [dict for dict in list_of_dicts.items() if dict['id'] == outside_input]
最后一个效率低下,仅出于学术原因:D
我想你可以再买一本字典
allDict = {1:{"id": 1, "content": "content of the first dict"},2:{"id": 2, "content": "content of the second dict"},3:{"id": 3, "content": "content of the third dict"}}
out = 1
result = allDict.get(out)
dict_one = {"id": 1, "content": "content of the first dict"}
dict_two = {"id": 2, "content": "content of the second dict"}
dict_three = {"id": 3, "content": "content of the third dict"}
dicts = {1: dict_one, 2: dict_two, 3: dict_three}
outside_input = 1
result = dicts.get(outside_input)
如果我有两个字典,并且我想根据外部输入将其分配给另一个变量,是否有更 Pythonic 的方式来完成它。
dict_one = {"id": 1, "content": "content of the first dict"}
dict_two = {"id": 2, "content": "content of the second dict"}
dict_three = {"id": 3, "content": "content of the third dict"}
#insert many more dicts....
outside_input = 1
if outside_input == 1:
result = dict_one
elif outside_input == 3:
result = dict_three
如果没有关于您的问题的更多详细信息,我可能会使用嵌套字典,例如:
dict_of_dicts = {
'dict_one': {"id": 1, "content": "content of the first dict"},
'dict_two': {"id": 2, "content": "content of the second dict"}
}
outside_input = 'dict_one'
result = dict_of_dicts[outside_input]
或者,如果字典中的 id 恰好存在,出于这个原因,您可以将其拉出作为键以减少冗余:
dict_of_dicts = {
1: {"content": "content of the first dict"},
2: {"content": "content of the second dict"}
}
还是第三种方式,但在搜索特定词典时速度较慢
list_of_dicts = [
{"id": 1, "content": "content of the first dict"},
{"id": 2, "content": "content of the second dict"}
]
outside_input = 'dict_one'
result = [dict for dict in list_of_dicts.items() if dict['id'] == outside_input]
最后一个效率低下,仅出于学术原因:D
我想你可以再买一本字典
allDict = {1:{"id": 1, "content": "content of the first dict"},2:{"id": 2, "content": "content of the second dict"},3:{"id": 3, "content": "content of the third dict"}}
out = 1
result = allDict.get(out)
dict_one = {"id": 1, "content": "content of the first dict"}
dict_two = {"id": 2, "content": "content of the second dict"}
dict_three = {"id": 3, "content": "content of the third dict"}
dicts = {1: dict_one, 2: dict_two, 3: dict_three}
outside_input = 1
result = dicts.get(outside_input)