为了账号安全,请及时绑定邮箱和手机立即绑定

字典中的Python列表

字典中的Python列表

繁花如伊 2021-03-26 14:10:54
现在,我知道已经对该主题进行了一些介绍,但我听不懂。我将向您展示我的代码,希望这可以帮助您了解我的位置。字典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)。elif choice == "6":    son = input("which grandfather and son pair need updating: ")    if son in father:        description = input("What's name of the grandfather?: ")        son[father] = description[1]        print("\n", son, "has been redefined.")    else:        print("\nThat person doesn't exist!  Try adding it.")任何帮助,将不胜感激。
查看完整描述

2 回答

?
开心每一天1111

TA贡献1836条经验 获得超13个赞

我想你是说

father[son][1] = description

在第二个片段中。

解释:

  • father是dict,son是字符串,因此son[father]会引发AttributeError

  • description也是一个字符串,description[1]单个字符也是如此。father[son]另一方面,是一个列表,您可以将其元素分配给新字符串。


查看完整回答
反对 回复 2021-03-30
?
慕码人8056858

TA贡献1803条经验 获得超6个赞

description = input("What's name of the grandfather?: ")

son[father] = description

因为描述是一个字符串


查看完整回答
反对 回复 2021-03-30
  • 2 回答
  • 0 关注
  • 133 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号