【发布时间】:2018-07-02 17:44:35
【问题描述】:
words = {'apple', 'plum', 'pear', 'peach', 'orange', 'cherry', 'quince'}
d = {}
for x in sorted(words):
if x not in d:
d[len(x)]=x
d[len(x)].append(x)
print(d)
AttributeError: 'str' object has no attribute 'append'
该程序的目标是拥有多个键,以字长(即 4、5 或 6 个字母)区分,用于存储按字母顺序排列的值:
{4: '梨', '李子' 5: '苹果', '桃子' 6: '樱桃', '橙子', '木瓜'}
我在向键添加项目时遇到问题。我目前得到的输出是(没有附加行):
{4: '李子', 5: '桃子', 6: '木瓜'}
所以它似乎正在删除以前的循环条目。更新和追加命令返回错误。
【问题讨论】:
标签: python