简体   繁体   English

计算不同的字典值

[英]Counting distinct dictionary values

I have this dictionary (key,list) 我有这本dictionary (key,list)

index={'chair':['one','two','two','two'],'table':['two','three','three']}

and i want this 我想要这个

#1. number of times each value occurs in each key. ordered descending
indexCalc={'chair':{'two':3,'one':1}, 'table':{'three':2,'two':1}}
#2. value for maximum amount for each key
indexMax={'chair':3,'table':2}
#3. we divide each value in #1 by value in #2 
indexCalcMax={'chair':{'two':3/3,'one':1/3}, 'table':{'three':2/2,'two':1/2}}

I think I should use lambda expressions, but can't come up with any idea how i can do that. 我想我应该使用lambda表达式,但不知道我怎么能这样做。 Any help? 有帮助吗?

First, define your values as lists correctly: 首先,将您的值正确定义为列表:

index = {'chair': ['one','two','two','two'], 'table': ['two','three','three']}

Then use collections.Counter with dictionary comprehensions: 然后使用collections.Counter和字典理解:

from collections import Counter
  1. number of times each value occurs in each key. 每个键中出现每个值的次数。
res1 = {k: Counter(v) for k, v in index.items()}
  1. value for maximum amount for each key 每个键的最大金额值
res2 = {k: v.most_common()[0][1] for k, v in res1.items()}
  1. we divide each value in #1 by value in #2 我们将#1中的每个值除以#2中的值
res3 = {k: {m: n / res2[k] for m, n in v.items()} for k, v in res1.items()}
index={'chair':{'one','two','two','two'},'table':{'two','three','three'}}

Problem: {} is creating a set. 问题: {}正在创建一个集合。 So you should consider to convert it into list. 所以你应该考虑将其转换为列表。

Now coming to your solution: 现在来解决你的问题:

from collections  import Counter


index={'chair': ['one','two','two','two'],'table':['two','three','three']}
updated_index = {'chair': dict(Counter(index['chair'])), 'table': dict(Counter(index['table']))}
updated_index_2 = {'chair': Counter(index['chair']).most_common()[0][1], 'table': Counter(index['table']).most_common()[0][1]}
print(updated_index)
print(updated_index_2)

You can use python collections library, Counter to find the count without writing any lambda function. 您可以使用python集合库, Counter来查找计数而无需编写任何lambda函数。

{'chair': {'one': 1, 'two': 3}, 'table': {'two': 1, 'three': 2}} {'chair':{'one':1,'two':3},'table':{'two':1,'three':2}}

{'chair': 3, 'table': 2} {'chair':3,'table':2}

Firstly, you have a mistake in how you created the index dict. 首先,您在创建index字典时遇到了错误。 You should have lists as the elements for each dictionary, you currently have sets. 您应该将列表作为每个字典的元素,您当前有集合。 Sets are automatically deduplicated, so you will not be able to get a proper count from there. 集合会自动进行重复数据删除,因此您无法从中获得正确的计数。

You should correct index to be: 您应该将索引更正为:

index={'chair':['one','two','two','two'],'table':['two','three','three']}

You can use the Counter module in Python 3, which is a subclass of the dict module, to generate what you want for each entry in indexCalc . 您可以使用Python 3中的Counter模块 (它是dict模块的子类)来为indexCalc每个条目生成indexCalc A counter will create a dictionary with a key, and the number of times that key exists in a collection. 计数器将创建一个包含密钥的字典,以及该密钥在集合中存在的次数。

indexCalc = {k, Counter(v) for k, v in index}

indexCalc looks like this: indexCalc看起来像这样:

{'chair': Counter({'two': 3, 'one': 1}), 'table': Counter({'three': 2, 'two': 1})}

We can easily find the index that corresponds to the maximum value in each sub-dictionary: 我们可以很容易地找到与每个子字典中的最大值对应的索引:

indexMax = {k: max(indexCalc[k].values()) for k in indexCalc}

indexMax looks like this: indexMax看起来像这样:

{'chair': 3, 'table': 2}

You can create indexCalcMax with the following comprehension, which is a little ugly: 您可以使用以下理解创建indexCalcMax ,这有点难看:

indexCalcMax = {k: {val: indexCalc[k][val] / indexMax[k] for val in indexCalc[k]} for k in indexCalc}

which is a dict-comprehension translation of this loop: 这是这个循环的字典理解翻译:

for k in indexCalc:
  tmp = {}
  for val in indexCalc[k]:
    tmp[val] = indexCalc[k][val] / float(indexMax[k])
  indexCalcMax[k] = tmp

I know this is suboptimal, but I had to do it as a thought exercise: 我知道这不是最理想的,但我必须做一个思考练习:

indexCalc = {
    k: {key: len([el for el in index[k] if el == key]) for key in set(index[k])} 
    for k in index
}

Not exactly lambda, as suggested, but comprehensions... Don't use this code in production :) This answer is only partial, you can use the analogy and come up with the other two structures that you require. 不完全是lambda,正如所建议的那样,但是理解......不要在生产中使用这个代码:)这个答案只是部分的,你可以使用类比并提出你需要的其他两个结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM