简体   繁体   中英

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?
  • wouldn't mind if only list of occurrences were returned i,e 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:

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 .

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})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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