【发布时间】:2020-12-12 20:29:12
【问题描述】:
尝试将浮点值附加到 defaultdic 类型字典中的相同键。
from collection import defaultdict
new_dict= defaultdict(list)
for row in list_dict:
acct=row[acct]
time_spent= float(row[time_spent])
if (acct not in new_dict):
new_dict[acct] = time_spent
else:
new_dict[acct].append(time_spent)
给我错误:
AttributeError: 'float' object has no attribute 'append'
如果我删除浮动,
time_spent= float(row[time_spent])
给我,
AttributeError: 'str' object has no attribute 'append'
我必须稍后将这些 time_spent 添加到值列表中,所以我希望它们是浮动的。
【问题讨论】:
标签: python python-3.x dictionary