函数使用If条件遍历字典

时间:2018-08-14 11:25:53

标签: python python-3.x

我对以下代码有疑问,基本上是一个带有2个参数的函数,一个是列,一个是数据帧,而我很难理解遍历该列的for循环并检查是否列中的值是空字典“ langs_counts”中的键,如何检查何时为空字典,请向我解释for循环以及为什么将“ entry”放在方括号之间。

for entry in col:
   if entry in langs_count.keys():
            langs_count[entry] += 1
   else:
            langs_count[entry] = 1

    # Define count_entries()
    def count_entries(df, col_name):
    """Return a dictionary with counts of 
    occurrences as value for each key."""

    # Initialize an empty dictionary: langs_count
        langs_count = {}

        # Extract column from DataFrame: col
        col = df[col_name]

        # Iterate over lang column in DataFrame
        for entry in col:
            # If the language is in langs_count, add 1
            if entry in langs_count.keys():
                langs_count[entry] += 1
            # Else add the language to langs_count, set the value to 1
            else:
                langs_count[entry] = 1


    # Return the langs_count dictionary
    return langs_count   

    # Call count_entries(): result
    result = count_entries(tweets_df, 'lang')

    # Print the result
    print(result)

1 个答案:

答案 0 :(得分:1)

为什么我们将“ entry”放在方括号之间?

方括号中的东西称为钥匙。

这只是索引的简化:

示例代码:

a = {'a':1,'b':2,'c':[1,2]}

a [“ d”] = 5在机器中变为:a .__ setitem __(“ d”,5)

如果“ d”不作为键存在,它将创建一个。如果存在,则该函数为其分配一个新值