如何让用户输入引用与输入同名的字典名称?
How can I get user input to reference the name of a dictionary with the same name as the input?
我正在尝试创建一个程序来比较字典并根据字典中设置的首选项选择最佳游戏选项。这涉及用户输入参与的人名列表,然后程序必须获取这些名称,获取这些名称的字典并进行比较。
但我不知道如何将用户输入的名称列表(我猜是 str 类型)变成字典变量的名称....这就是我所拥有的远:
Morgan = {
"Social Deduction" : 5,
"Deck Building" : 2,
"Dice games" : 3,
"classic Card Games" : 5,
"Simple Party Games" : 5,
"Strategy" : 3,
"Deduction" : 4,
"Blacklist" : "Deception"
}
Adam = {
"Social Deduction" : 5,
"Deck Building" : 5,
"Dice games" : 5,
"classic Card Games" : 5,
"Simple Party Games" : 5,
"Strategy" : 5,
"Deduction" : 5,
}
Guy = {
"Social Deduction" : 1,
"Deck Building" : 2,
"Dice games" : 5,
"classic Card Games" : 5,
"Simple Party Games" : 2,
"Strategy" : 3,
"Deduction" : 4,
}
def main():
accepted = input('Who is coming today? ').split(",")
for i in range(len(accepted)-1):
print(i)
if i==0:
print(cumulativeComp = accepted[i].items() & accepted[i+1].items())
elif i < len(accepted)-1:
print(cumulativeComp = cumulativeComp.items() & accepted[i+1].items())
else:
pass
if __name__== "__main__":
main()
有什么想法或建议吗?
制作字典的字典:
game_preferences = {
"Morgan" : {
"Social Deduction" : 5,
"Deck Building" : 2,
"Dice games" : 3,
"classic Card Games" : 5,
"Simple Party Games" : 5,
"Strategy" : 3,
"Deduction" : 4,
"Blacklist" : "Deception"
},
"Adam" : {
"Social Deduction" : 5,
"Deck Building" : 5,
"Dice games" : 5,
"classic Card Games" : 5,
"Simple Party Games" : 5,
"Strategy" : 5,
"Deduction" : 5,
},
"Guy" : {
"Social Deduction" : 1,
"Deck Building" : 2,
"Dice games" : 5,
"classic Card Games" : 5,
"Simple Party Games" : 2,
"Strategy" : 3,
"Deduction" : 4,
},
}
最好将这些词典放入一个主词典中,例如 Alex 的。
要回答您的问题,您可以使用 globals()
:
Morgan = {
"Social Deduction" : 5,
"Deck Building" : 2,
"Dice games" : 3,
"classic Card Games" : 5,
"Simple Party Games" : 5,
"Strategy" : 3,
"Deduction" : 4,
"Blacklist" : "Deception"
}
def main():
print(globals()['Morgan']
main()
我正在尝试创建一个程序来比较字典并根据字典中设置的首选项选择最佳游戏选项。这涉及用户输入参与的人名列表,然后程序必须获取这些名称,获取这些名称的字典并进行比较。
但我不知道如何将用户输入的名称列表(我猜是 str 类型)变成字典变量的名称....这就是我所拥有的远:
Morgan = {
"Social Deduction" : 5,
"Deck Building" : 2,
"Dice games" : 3,
"classic Card Games" : 5,
"Simple Party Games" : 5,
"Strategy" : 3,
"Deduction" : 4,
"Blacklist" : "Deception"
}
Adam = {
"Social Deduction" : 5,
"Deck Building" : 5,
"Dice games" : 5,
"classic Card Games" : 5,
"Simple Party Games" : 5,
"Strategy" : 5,
"Deduction" : 5,
}
Guy = {
"Social Deduction" : 1,
"Deck Building" : 2,
"Dice games" : 5,
"classic Card Games" : 5,
"Simple Party Games" : 2,
"Strategy" : 3,
"Deduction" : 4,
}
def main():
accepted = input('Who is coming today? ').split(",")
for i in range(len(accepted)-1):
print(i)
if i==0:
print(cumulativeComp = accepted[i].items() & accepted[i+1].items())
elif i < len(accepted)-1:
print(cumulativeComp = cumulativeComp.items() & accepted[i+1].items())
else:
pass
if __name__== "__main__":
main()
有什么想法或建议吗?
制作字典的字典:
game_preferences = {
"Morgan" : {
"Social Deduction" : 5,
"Deck Building" : 2,
"Dice games" : 3,
"classic Card Games" : 5,
"Simple Party Games" : 5,
"Strategy" : 3,
"Deduction" : 4,
"Blacklist" : "Deception"
},
"Adam" : {
"Social Deduction" : 5,
"Deck Building" : 5,
"Dice games" : 5,
"classic Card Games" : 5,
"Simple Party Games" : 5,
"Strategy" : 5,
"Deduction" : 5,
},
"Guy" : {
"Social Deduction" : 1,
"Deck Building" : 2,
"Dice games" : 5,
"classic Card Games" : 5,
"Simple Party Games" : 2,
"Strategy" : 3,
"Deduction" : 4,
},
}
最好将这些词典放入一个主词典中,例如 Alex 的
要回答您的问题,您可以使用 globals()
:
Morgan = {
"Social Deduction" : 5,
"Deck Building" : 2,
"Dice games" : 3,
"classic Card Games" : 5,
"Simple Party Games" : 5,
"Strategy" : 3,
"Deduction" : 4,
"Blacklist" : "Deception"
}
def main():
print(globals()['Morgan']
main()