词典中的Python列表
现在我知道已经有一些关于这个话题的报道了,但我不能理解。我将向您展示我的代码,我希望这有助于您了解我所处的位置 字典词典中的Python列表,python,Python,现在我知道已经有一些关于这个话题的报道了,但我不能理解。我将向您展示我的代码,我希望这有助于您了解我所处的位置 字典 father = {"Yin": ["yang","yaha"]} 这段代码运行良好 elif choice == "5": son = input("Enter the name of a son to get the name of his grandfather: ") if son in father: description = fat
father = {"Yin": ["yang","yaha"]}
这段代码运行良好
elif choice == "5":
son = input("Enter the name of a son to get the name of his grandfather: ")
if son in father:
description = father[son]
print("\n", son, "'s grandfather is", description[1])
else:
print("\nSorry, I don't know who that is", son)
这段代码没有,我只是希望它能够更改列表中的第二项(yaha)
任何帮助都将不胜感激。我想你是说
father[son][1] = description
在第二段中
说明:
是一个dict,father
是一个字符串,因此son
将引发一个son[father]
AttributeError
也是字符串,因此description
是单个字符<另一方面,code>father[son]是一个列表,您可以将其元素分配给一个新字符串description[1]
因为描述是一个字符串OMG!谢谢,我试着把(1)几乎放在其他地方。“吉米莱特欢迎你:”考虑它是否解决了这个问题。
father[son][1] = description
description = input("What's name of the grandfather?: ")
son[father] = description