简体   繁体   English

如何将列的唯一值和 append 每个值计算到字典中?

[英]How to count the unique values of a column and append each value to a dictionary?

The tutorial suggests this solution本教程建议使用此解决方案

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) 

The output is output 是

{'17+': 622, '4+': 4433, '9+': 987, '12+': 1155}

I just want to understand the code logic.我只是想了解代码逻辑。 The line says if c_rating in content_ratings: is supposed to return False as it doesn't meet the condition since the content_rating itself is empty so c_rating is not in the dictionary.该行说if c_rating in content_ratings:应该返回False因为它不满足条件,因为content_rating本身是空的,所以c_rating不在字典中。 How does this logic find the content rating and append it to the dictionary?这个逻辑如何在字典中找到内容分级和 append?

Both lines in if-else statement looks same, but there is slight difference, + sign: if-else 语句中的两行看起来相同,但略有不同, +号:

if c_rating in content_ratings:
    content_ratings[c_rating] += 1
else:
    content_ratings[c_rating] = 1

content_ratings[c_rating] = 1 this is how an element is added into dictionary in python. content_ratings[c_rating] = 1这是在 python 中将元素添加到字典中的方式。 Also you can update existed element dictionary in same way;您也可以以相同的方式更新现有的元素字典; here content_ratings[c_rating] += 1 you get the existing element and increase its value.在这里content_ratings[c_rating] += 1你得到了现有的元素并增加了它的价值。

So, you are right, when if c_rating in content_ratings: returns false , it adds elements, otherwise, it updates its value (increasing count).所以,你是对的, if c_rating in content_ratings:返回false ,它会添加元素,否则,它会更新它的值(增加计数)。

You can see basic operation on dictionary in docs.您可以在文档中查看字典的基本操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算数据框中另一列中每个唯一值对应的值? - How to count the values corresponding to each unique value in another column in a dataframe? 如何根据字典值计算 pandas 列中的唯一值 - How to count unique values in pandas column base on dictionary values Python:计算具有重复值的列中每个唯一值的第一个实例 - Python: Count the First Instance of each Unique Value in a Column with Repeating Values 如何使用python计算一列中每一行的唯一值? - How to count the unique values of each row in one column with python? 如何获取 pandas 中每对唯一列的列值计数? - How to get count of column values for each unique pair of columns in pandas? 如何对 Pandas 中的列中的每个唯一值进行排序和计数 - How to sort and count each unique value in a column in Pandas 如何计算一列列表中每个唯一值的出现次数 Pandas - How to count occurrences of each unique value within a column of lists Pandas 如何计算包含列表的字典中每个键的每个唯一值? - How do I count each unique value for each key in a dictionary containing lists? 创建包含Excel列中每个唯一项计数的字典 - Create dictionary with count of each unique item from an excel column 如何使用for循环将列值添加到数据框字典中,以便每个数据框都获得一个唯一的列? - How to add a column value into dataframe dictionary using a for loop so that each dataframe gets a unique column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM