【发布时间】:2022-11-22 04:11:45
【问题描述】:
例如有一个列表:
list = [{'brand': 'Ford', 'Model': 'Mustang', 'year': 1964}, {'brand': 'Nissan', 'model': 'Skyline', 'year': 1969} ...]
我想数一数每个模型有多少个模型。我该怎么做?
顺便说一句,对于格式错误我很抱歉,我是新来的。
我试过这个方法:
model_count = {}
for i in list:
if i['Model'] in model_count:
model_count[i] += 1
else:
model_count[i] = 1
我得到了这个错误:TypeError: unhashable type: 'dict'
【问题讨论】:
-
我想你的意思是
model_count[i['Model']] += 1等等
标签: python list dictionary count each