Indicate how many times a dictionary value with more than one key was found

I am working on python. Is there a way to count how many times the values โ€‹โ€‹in the dictionary are found with more than one key, and then return the score?

So, if I had 50 values โ€‹โ€‹and I ran a script to get this, I would get an account that would look something like this:

1: 23 2: 15 3: 7 4: 5 

It is said above that 23 values โ€‹โ€‹are displayed in 1 key, 15 values โ€‹โ€‹are displayed in 2 keys, 7 values โ€‹โ€‹are displayed in 3 keys and 5 values โ€‹โ€‹are displayed in 4 keys.

Also, would this question change if there were several values โ€‹โ€‹for the key in my dictionary?

Here is an example of my dictionary (these are the names of bacteria):

{'0': ['Pyrobaculum'], '1': ['Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium'], '3': ['Thermoanaerobacter', 'Thermoanaerobacter'], '2': ['Helicobacter', 'Mycobacterium'], '5': ['Thermoanaerobacter', 'Thermoanaerobacter'], '4': ['Helicobacter'], '7': ['Syntrophomonas'], '6': ['Gelria'], '9': ['Campylobacter', 'Campylobacter'], '8': ['Syntrophomonas'], '10': ['Desulfitobacterium', 'Mycobacterium']}

So, from this example there are 8 unique values, I have the perfect feedback that I would get:

 1:4 2:3 3:1 

So, 4 bacteria names are found in only one key, 3 bacteria are found in two keys, and 1 bacteria are found in three keys.

+4
source share
3 answers

Therefore, if I do not read this incorrectly, you want to know:

  • For each of the values โ€‹โ€‹in the source dictionary, how many times does each different value counter take place?
  • In essence, you want frequency values โ€‹โ€‹in a dictionary

I took a less elegant approach, which answered the others, but violated the problem for you in separate steps:

 d = {'0': ['Pyrobaculum'], '1': ['Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium', 'Mycobacterium'], '3': ['Thermoanaerobacter', 'Thermoanaerobacter'], '2': ['Helicobacter', 'Mycobacterium'], '5': ['Thermoanaerobacter', 'Thermoanaerobacter'], '4': ['Helicobacter'], '7': ['Syntrophomonas'], '6': ['Gelria'], '9': ['Campylobacter', 'Campylobacter'], '8': ['Syntrophomonas'], '10': ['Desulfitobacterium', 'Mycobacterium']} # Iterate through and find out how many times each key occurs vals = {} # A dictonary to store how often each value occurs. for i in d.values(): for j in set(i): # Convert to a set to remove duplicates vals[j] = 1 + vals.get(j,0) # If we've seen this value iterate the count # Otherwise we get the default of 0 and iterate it print vals # Iterate through each possible freqency and find how many values have that count. counts = {} # A dictonary to store the final frequencies. # We will iterate from 0 (which is a valid count) to the maximum count for i in range(0,max(vals.values())+1): # Find all values that have the current frequency, count them #and add them to the frequency dictionary counts[i] = len([x for x in vals.values() if x == i]) for key in sorted(counts.keys()): if counts[key] > 0: print key,":",counts[key] 

You can also check this code on code code .

+5
source

If I understand correctly, you want to count the number of dictionary values. If the values โ€‹โ€‹are counted on collections.Counter , you just need to call Counter in the dictionaries, and then again on the first counter values. Here is an example of using a dictionary where the keys are range(100) and the values โ€‹โ€‹are random between 0 and 10:

 from collections import Counter d = dict(enumerate([str(random.randint(0, 10)) for _ in range(100)])) counter = Counter(d.values()) counts_counter = Counter(counter.values()) 

EDIT

After the sample dictionary has been added to the question, you need to do the first count a little differently ( d is the dictionary in the question):

 from collections import Counter c = Counter() for v in d.itervalues(): c.update(set(v)) Counter(c.values()) 
+5
source

You can use counter

 >>>from collections import Counter >>>d = dict(((1, 1), (2, 1), (3, 1), (4, 2), (5, 2), (6, 3), (7, 3))) >>>d {1: 1, 2: 1, 3: 1, 4: 2, 5: 2, 6: 3, 7: 3} >>>Counter(d.values()) Counter({1: 3, 2: 2, 3: 2}) 
+2
source

Source: https://habr.com/ru/post/1500220/


All Articles