我有一个Python字典对象,看起来像这样:
[{"house": 4, "sign": "Aquarius"},
{"house": 2, "sign": "Sagittarius"},
{"house": 8, "sign": "Gemini"},
{"house": 3, "sign": "Capricorn"},
{"house": 2, "sign": "Sagittarius"},
{"house": 3, "sign": "Capricorn"},
{"house": 10, "sign": "Leo"},
{"house": 4, "sign": "Aquarius"},
{"house": 10, "sign": "Leo"},
{"house": 1, "sign": "Scorpio"}]
现在针对每个“sign”键,我想计算每个值出现的次数。
def predominant_sign(data):
signs = [k['sign'] for k in data if k.get('sign')]
print len(signs)
然而,这会打印出字典中“sign”出现的次数,而不是获取sign
的值并计算特定值出现的次数。
例如,我想要看到的输出是:
Aquarius: 2
Sagittarius: 2
Gemini: 1
...
等等。为了得到所需的输出,我应该改变什么?