本教程建议使用此解决方案
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
content_ratings = {}
for row in apps_data[1:]:
c_rating = row[10] #the content rating that says which app is suitable for which age
if c_rating in content_ratings:
content_ratings[c_rating] += 1
else:
content_ratings[c_rating] = 1
print(content_ratings)
输出是
{'17+': 622, '4+': 4433, '9+': 987, '12+': 1155}
我只是想了解代码逻辑。该行说if c_rating in content_ratings:应该返回False,因为它不符合条件,因为它content_rating本身是空的,所以c_rating不在字典中。这个逻辑如何找到内容评级并将其附加到字典中?
弑天下
相关分类