Count在字典值中加入字符串

时间:2018-03-19 11:17:30

标签: python dictionary

鉴于我的词典列表

def count_names(dict_nomi):
  names = Counter(v['first_name'] for v in dict_nomi if v.get('first_name'))
  for names, count in names.most_common():
    print(names, count)

count_names(dict_nomi)

我想计算值的出现不是一个键,而是多个键(在这种情况下是两个)。

('Luca', 3)
('Stefano', 1)

这给了我:

('Luca Rossi', 2)
('Stefano De Rosso', 1)
('Luca Bianchi', 1)

但我怎么能得到像

这样的东西
{
  "college": [
    {
      "colleges": [],
      "department": [
        1,
        2,
        3
      ],
      "general_course": [],
      "id": 1,
      "name": "College of the Arts",
      "short_name": "",
      "url": "/content.php?catoid=16&navoid=1919"
    },
    {
      "colleges": [],
      "department": [
        4,
        5,
        6
      ],
      "general_course": [],
      "id": 2,
      "name": "College of Communications",
      "short_name": "",
      "url": "/content.php?catoid=16&navoid=1920"
    },
    {
      "colleges": [],
      "department": [
        7,
        12
      ],
      "general_course": [],
      "id": 3,
      "name": "College of Education",
      "short_name": "",
      "url": "/content.php?catoid=16&navoid=1921"
    },
    {
      "colleges": [],
      "department": [
        13,
        17,
        19
      ],
      "general_course": [],
      "id": 4,
      "name": "College of Engineering and Computer Science",
      "short_name": "",
      "url": "/content.php?catoid=16&navoid=1922"
    },
    {
      "colleges": [],
      "department": [
        20,
        26,
        27
      ],
      "general_course": [],
      "id": 5,
      "name": "College of Health and Human Development",
      "short_name": "",
      "url": "/content.php?catoid=16&navoid=1923"
    },
    {
      "colleges": [],
      "department": [
        28,
        29,
        32,
        48
      ],
      "general_course": [],
      "id": 6,
      "name": "College of Humanities and Social Sciences",
      "short_name": "",
      "url": "/content.php?catoid=16&navoid=1924"
    },
    {
      "colleges": [],
      "department": [
        52,
        57
      ],
      "general_course": [],
      "id": 7,
      "name": "College of Natural Sciences and Mathematics",
      "short_name": "",
      "url": "/content.php?catoid=16&navoid=1925"
    },
    {
      "colleges": [],
      "department": [
        58,
        59,
        63
      ],
      "general_course": [],
      "id": 8,
      "name": "Mihaylo College of Business and Economics",
      "short_name": "",
      "url": "/content.php?catoid=16&navoid=1926"
    }
  ]
}

此?

感谢。

5 个答案:

答案 0 :(得分:3)

仅仅是:

from collections import Counter

dict_nomi = [
{'first_name': 'Luca', 'last_name': 'Rossi'},
{'first_name': 'Stefano', 'last_name': 'De Rosso'},
{'first_name': 'Luca', 'last_name': 'Bianchi'},
{'first_name': 'Luca', 'last_name': 'Rossi'},
]
c = Counter(' '.join((d['first_name'], d['last_name'])) for d in dict_nomi)
print(c)

输出:

Counter({'Luca Rossi': 2, 'Stefano De Rosso': 1, 'Luca Bianchi': 1})

答案 1 :(得分:1)

def count_names(dict_nomi):
  names = Counter("{} {}".format(v['first_name'], v['last_name']) for v in dict_nomi if v.get('first_name') and v.get('last_name'))
  for names, count in names.most_common():
      print(names, count)
  return names

names = count_names(dict_nomi)

答案 2 :(得分:1)

from collections import Counter

Counter([d['first_name'] + ' ' + d['last_name'] for d in dict_nomi])
out: Counter({'Luca Rossi': 2, 'Stefano De Rosso': 1, 'Luca Bianchi': 1})

答案 3 :(得分:0)

dict_nomi = [
{'first_name': 'Luca', 'last_name': 'Rossi'},
{'first_name': 'Stefano', 'last_name': 'De Rosso'},
{'first_name': 'Luca', 'last_name': 'Bianchi'},
{'first_name': 'Luca', 'last_name': 'Rossi'},
]

res = {}
for i in dict_nomi:   #Iterate over your dict
    val =  "{0} {1}".format(i["first_name"], i["last_name"])
    if val not in res:   #Check if key in res
        res[val] = 1     #Else create and add count
    else:
        res[val] += 1    #Increment count
print(res)

<强>输出:

{'Luca Bianchi': 1, 'Luca Rossi': 2, 'Stefano De Rosso': 1}

答案 4 :(得分:0)

为此,您可以使用python中 collections 模块的Counter Dict。

from collections import Counter as cnt

dict_nomi = [
{'first_name': 'Luca', 'last_name': 'Rossi'},
{'first_name': 'Stefano', 'last_name': 'De Rosso'},
{'first_name': 'Luca', 'last_name': 'Bianchi'},
{'first_name': 'Luca', 'last_name': 'Rossi'},
]

cnt_dict = cnt(' '.join((dic['first_name'], dic['last_name'])) for dic in dict_nomi)

final_res = [(i,j,) for i,j in cnt_dict.iteritems()]

输出

[('Luca Bianchi', 1), ('Luca Rossi', 2), ('Stefano De Rosso', 1)]

如果这可以解决您的问题,请告诉我