【发布时间】:2016-10-15 19:26:07
【问题描述】:
我目前正在制作一个猜猜谁喜欢学校作业的游戏,但我似乎无法让它发挥作用。我想要做的是从列表中的所有字典中打印字段“名称”。
Greg = {"Name":"Greg", "HairLength":"Short", "HairColour":"Brown", "FacialHair":"Yes", "Jewellery":"Yes", "Hat":"No", "Lipstick":"No", "Gender":"Male"}
Chris = {"Name":"Chris", "HairLength":"Long", "HairColour":"Blonde", "FacialHair":"No", "Jewellery":"No","Hat":"Yes", "Lipstick":"Yes", "Gender":"Male"}
Jason = {"Name":"Jason", "HairLength":"Short", "HairColour":"Brown", "FacialHair":"Yes", "Jewellery":"No","Hat":"Yes", "Lipstick":"No", "Gender":"Male"}
Clancy = {"Name":"Clancy", "HairLength":"Bald", "HairColour":"Red", "FacialHair":"Yes", "Jewellery":"No", "Hat":"No","Lipstick":"No", "Gender":"Male"}
Betty = {"Name":"Betty", "HairLength":"Short", "HairColour":"Blonde", "FacialHair":"No", "Jewellery":"Yes","Hat":"Yes", "Lipstick":"Yes", "Gender":"Female"}
Helen = {"Name":"Helen", "HairLength":"Short", "HairColour":"Brown", "FacialHair":"No", "Jewellery":"No", "Hat":"No","Lipstick":"Yes", "Gender":"Female"}
Selena = {"Name":"Selena", "HairLength":"Long", "HairColour":"Brown", "FacialHair":"No", "Jewellery":"Yes","Hat":"No", "Lipstick":"No", "Gender":"Female"}
Jacqueline = {"Name":"Jacqueline", "HairLength":"Long", "HairColour":"Red", "FacialHair":"Yes", "Jewellery":"Yes", "Hat":"No","Lipstick":"No", "Gender":"Female"}
AISuspects = ([Greg, Chris, Jason, Clancy, Betty, Selena, Helen,
Jacqueline])
UserSuspects = ([Greg, Chris, Jason, Clancy, Betty, Selena, Helen, Jacqueline])
print("AISuspects:")
#Here i want it to print the field "Name" in every dictionary within the list AISuspects
print("UserSuspects:")
#Here i want it to print the field "Name" in every dictionary within the list UserSuspects
求解后的预期输出和当前输出:
人工智能嫌疑人: [“格雷格”、“克里斯”、“杰森”、“克兰西”、“贝蒂”、“赛琳娜”、“海伦”、“杰奎琳”]
用户怀疑: [“格雷格”、“克里斯”、“杰森”、“克兰西”、“贝蒂”、“赛琳娜”、“海伦”、“杰奎琳”]
【问题讨论】:
-
您的
AISuspects和UserSuspects列表不需要括号()。只需使用[]创建列表就足够了。
标签: python list dictionary printing output