Python count of items in a dictionary of lists

后端 未结 2 1782
面向向阳花
面向向阳花 2021-01-18 05:11

I have a dictionary of lists for which I want to add a value to a particular list... I have the following dictionary of lists.

d = {\'a\': [4,\'Adam\', 2], \         


        
2条回答
  •  余生分开走
    2021-01-18 05:24

    d = {'key': 'value'}
    temp_dict = {}
    for key, values in d.items():
        if values[1] in temp_dict:
            temp_dict[values[1]] = temp_dict[values[1]] + 1
        else:
            temp_dict[values[1]] = 1
    

    This code is longer than the previous answer, but it's just another way to produce the same results. Anyway, temp_dict will store the names as keys and values as the number of times it shows up.

提交回复
热议问题