gpt4 book ai didi

python - 单个单词 'string'能否转换成同名字典

转载 作者:行者123 更新时间:2023-12-01 08:16:22 24 4
gpt4 key购买 nike

我的程序以姓名列表开始。例如:['Bob','John','Mike']' 然后将该列表打乱为随机顺序。例如: ['Mike','Bob','John'] 然后从列表中取出一个名字。例如:['Mike'] 离开 ['Bob','John']

然后我想将此名称与同名的字典相关联。例如:'Mike' = {'姓氏':'琼斯','年龄':55,'员工 ID':101}

然后能够调用并打印所选名称的特定键:值。例如: print(Mike[Age])

(我目前的代码与这个例子类似)

list_of_names = ['Bob','John','Mary','Joan','Mike']
chosen_name = list_of_names.pop(0)
print("person chosen: ", (chosen_name))

# Dictionaries are pre-formatted waiting to be paired with their listed name
'Bob' = {'Surname' : 'Kelly', 'Age': 49, 'Staff ID': 86},
'John' = {'Surname' : 'Hogan', 'Age': 57, 'Staff ID': 22},
'Mike' = {'Surname' : 'Jones', 'Age': 55, 'Staff ID': 101},

# Prints the randomly chosen name and the dictionary associated with it.
print(chosen_name)

# Prints a Value for a particular key of that chosen name
print(chosen_name[Age])

我将非常感谢任何建议,甚至是实现这一目标的替代方法。非常感谢。

最佳答案

我想说,在代码中添加另一个字典可能是最简单的。例如:

list_of_names = ['Bob','John','Mary','Joan','Mike']
chosen_name = list_of_names.pop(0)
print("person chosen: ", (chosen_name))

# Dictionaries are pre-formatted waiting to be paired with their listed name
person_info = {}
person_info['Bob'] = {'Surname' : 'Kelly', 'Age': 49, 'Staff ID': 86}
person_info['John'] = {'Surname' : 'Hogan', 'Age': 57, 'Staff ID': 22}
person_info['Mike'] = {'Surname' : 'Jones', 'Age': 55, 'Staff ID': 101}

# Prints the randomly chosen name and the dictionary associated with it.
print(person_info[chosen_name])

# Prints a Value for a particular key of that chosen name
print(person_info[chosen_name]['Age'])

关于python - 单个单词 'string'能否转换成同名字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54964126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com