用字典中的键反转多个值 [英] Reverse multiple values with keys in dictionary
问题描述
我对Python和整体编程非常陌生,请耐心等待.
I'm quite new to Python and overall programming so bear with me.
我所拥有的是一个以['Male', 'Female', 'Eunuch']
作为值的字典,并以这些键的不同名称作为键:
What I have is a dictionary of ['Male', 'Female', 'Eunuch']
as values and different names for these as keys:
Persons = { 'Hodor' : 'Male', 'Tyrion': 'Male', 'Theon': 'Male', 'Arya': 'Female', 'Daenerys': 'Female', 'Sansa': 'Female', 'Varys': 'Eunuch}
我想将它们订购到{ Gender: {Names: Counts}}
因此:
输入:
lst = ['Hodor', 'Hodor', 'Tyrion', 'Tyrion', 'Tyrion', 'Arya', 'Daenerys', 'Daenerys', 'Varys']
输出:
{'Male': {'Hodor': 2, 'Tyrion': 3, Theon: 0}, 'Female': {'Arya': 1, 'Daenerys': 2, 'Sansa': 0}, 'Eunuch': {'Varys': 1}}
我尝试的第一件事是编写用于计数的代码:
The first thing I have tried is making a code for counting:
counts = {}
for key in Persons:
counts[key] = 0
for key in lst:
counts[key] += 1
我的字典D现在包含计数,但是如何将它们全部计算在一起?
My dictionary D now contains the counts, but how do I compute them all together?
Names = Persons.keys()
Gender = Persons.values()
Counts = counts.values()
Names = counts.keys()
如果未提及差异,则不应在输出中显示性别太监". 我尝试了不同的事情,但是当我尝试将它们连接起来时.我尝试用值切换键,但是只有一个名字出现.
If varys isn't mentioned the gender 'Eunuch' shouldn't be in the output. I've tried different things, but when I try to connect them. I try to switch keys with values, but then only one name comes up.
希望这对我想做的事情有意义:)
Hope it makes sense of what I want to do :)
如果没有提到Sansa并且其他女性是她,那么她的值应该为0.我希望能够处理数字.说出与所有男性相比,霍多尔提到的百分比是多少.
If Sansa isn't mention and other females are her value should be 0. And I want to be able to manipulate the numbers. Say at what percentage is Hodor mentioned compared to all the males.
推荐答案
考虑
from collections import Counter
cnt = Counter(lst)
print {gender: {name: cnt[name] for name in persons if persons[name] == gender}
for gender in set(persons.values())}
# {'Eunuch': {'Varys': 1},
# 'Male': {'Tyrion': 3, 'Hodor': 2, 'Theon': 0},
# 'Female': {'Daenerys': 2, 'Arya': 1, 'Sansa': 0}}
要计算百分比,我们添加一个辅助函数:
To calculate percentages let's add a helper function:
def percentage_dict(d):
s = float(sum(d.values()))
return {k: 100 * v / s for k, v in d.items()}
然后
print {gender: percentage_dict({name: cnt[name] for name in persons if persons[name] == gender})
for gender in set(persons.values())}
# {'Eunuch': {'Varys': 100.0}, 'Male': {'Hodor': 40.0, 'Theon': 0.0, 'Tyrion': 60.0}, 'Female': {'Daenerys': 66.66666666666667, 'Arya': 33.333333333333336, 'Sansa': 0.0}}
这是使用辅助函数更有效地编写此理解的方法:
And this is how to write this comprehension more efficiently using a helper function:
def invert(d):
"""Turn {a:x, b:x} into {x:[a,b]}"""
r = {}
for k, v in d.items():
r.setdefault(v, []).append(k)
return r
然后
cnt = Counter(lst)
print {gender: {name: cnt[name] for name in names}
for gender, names in invert(persons).items()}
排除总计为零的下标
print {gender: {name: cnt[name] for name in names}
for gender, names in invert(persons).items()
if any(cnt[name] for name in names)
}
这篇关于用字典中的键反转多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!