简体   繁体   English

字典中值(元组集)的出现次数将原始字典长度保留在输出列表/字典/元组中

[英]count occurence of value (tuple set) in dictionary keep original dictionary length in output list/dictionary/ tuple

I've seen this sort question asked many times but unfortunately for me this time it comes with a bit of a twist. 我已经看过很多次这样的问题了,但不幸的是,这次对我来说有点曲折。

I have a dictionary in the format: 我有一本格式的字典:

name: (job, score)

example: 例:

dict = {bob: (farmer, 9), sue: (farmer, 9), tim: (farmer, 5), jill, (chef, 8)}

now if i use: 现在,如果我使用:

x =  Counter(x for x in dict.values())

I'll get the list as expected (but not what I want): 我会按预期获得列表(但不是我想要的):

Counter({(farmer,9): 2, (farmer, 5): 1, (chef, 8): 1})

what i would really like is to see each name with the occurrences of their job and score like so: 我真正想要的是看到每个名字以及他们的工作经历和得分,如下所示:

Counter({bob:2, sue:2, tim:1, jill:1})

which is also to say that I would like the output dictionary length to be the same as the input dictionary length. 这也就是说我希望输出字典的长度与输入字典的长度相同。

Things I can change: 我可以改变的事情:

  • dictionary could maybe be set of nested tuples? 字典可能是嵌套元组的集合? ie (bob,(farmer,9)) if this helps? 即(bob,(farmer,9))是否有帮助?
  • wouldn't mind if only list of occurrences were returned i,e 2,2,1,1 不会介意是否只返回出现列表,即2,2,1,1
  • also wouldn't mind being told there's a much better way to do this. 也不会介意有更好的方法来做到这一点。

what im trying to do is make occurence the size of my bubble in a bubble chart. 我想做的是在气泡图中使气泡的大小发生。 I'd like to be able to extract a list of the same length of occurrences at the same index as described above. 我希望能够如上所述提取在相同索引处出现的相同长度的列表。

So far I have an equal list of jobs and scores, that third list containing occurrences would help make the graph more clear I think. 到目前为止,我的工作和成绩清单是相等的,我认为,包含事件的第三份清单将有助于使图表更加清晰。

Your dict omits '' around strings, which would result in an error at runtime. 您的字典在字符串周围省略了'',这会在运行时导致错误。 Hence: 因此:

dict = {'bob': ('farmer', 9), 'sue': ('farmer', 9), 'tim': ('farmer', 5), 'jill': ('chef', 8)}

Since the values in your dict are lists in identical format, you can just index them (just like any other list): 由于字典中的值是相同格式的列表,因此您可以将它们编入索引(就像其他任何列表一样):

for k, v in dict.items():
    print(k,v[1])

#OUTPUT:
tim 5
bob 9
jill 8
sue 9

First don't use dict as a variable name: 首先不要使用dict作为变量名:

You can try this approach without importing anything. 您可以尝试这种方法而无需导入任何内容。

dict_1 = {'bob': ('farmer', 9),'sue': ('farmer', 9), 'tim': ('farmer', 5), 'jill':('chef', 8)}

First group similar values: 第一组类似的值:

pre_data={}

for i,j in dict_1.items():
    if j not in pre_data:
        pre_data[j]=[i]
    else:
        pre_data[j].append(i)

Now catch the length of count 现在捕捉计数的长度

final_result={}
for i,j in pre_data.items():

    if len(j)>1:
        for sub_data in j:


            final_result[sub_data]=len(j)
    else:
        final_result[j[0]]=1
print(final_result)

output: 输出:

{'sue': 2, 'jill': 1, 'tim': 1, 'bob': 2}

You are half way there. 你在那儿。 Just link your 2 dictionaries and remember to never named variables after classes , eg use d not dict . 只需链接两个字典,并记住在类之后再也不要命名变量 ,例如,使用d not dict

from collections import Counter

d = {'bob': ('farmer', 9), 'sue': ('farmer', 9),
     'tim': ('farmer', 5), 'jill': ('chef', 8)}

x =  Counter(x for x in d.values())

res = Counter({k: x[v] for k, v in d.items()})

# Counter({'bob': 2, 'jill': 1, 'sue': 2, 'tim': 1})

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

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