简体   繁体   中英

How to count the number of repeats for an item in a list which is a value in a dictionary?

So I have a dictionary (dictionary2) where every value is a list. I need to make a function that shows this data on a plot, with the keys on the x-axis, which I managed. I want to create a separate list (count_list) that contains counts for how often each number repeats itself within the list belonging to a given key (only 1 separate list for all of the values of the dictionary together). The end goal is to create a scatterplot where overlapping markers are bigger, which I could do by ascribing this separate list to the 's' argument in the scatterplot call. (Note that different color or something would work fine too but that would still require the count_list anyway)

I have 1 working version that does not account for overlap (this might give some context, see below).

I'm currently trying to make the count_list with the following code (I took it outside of the function for experimenting convenience):

count_list=[]
for key in dictionary2:
    for value in dictionary2:
        for M in value:
            count= value.count(M)
            count_list.append(count)

This returns a count_list with the same sequence of numbers being repeated for each key. I realize my code is probably way too simplistic, so I am not surprised it didn't work. However, I'm not sure where to go from here and don't understand why the output looks like that.

The current plot looks like this:

def plot_dict(dataset):
    
    #transforming data to be read correctly into the plot
    d = dataset
    x= []
    y= []
    for k, v in d.items():
        x.extend(list(itertools.repeat(k, len(v))))
        y.extend(v)
    
    plt.figure(figsize=(30,30))
    plt.plot(x,y, '.')
    plt.title('FASTA plot', fontsize=45)
    plt.xlabel('Sequence IDs', fontsize=30)
    plt.ylabel('Molecular weight', fontsize=30)
    plt.xticks(fontsize=15)
    plt.yticks(fontsize=15)
plot_dict(dictionary2)

enter image description here

(I'm using jupyterlab 3.0.14.)

This is my first time posting a question in stack overflow, so if I broke any kind of etiquette or if anything in my explanation of the problem was unclear, please let me know!

I'm not sure if I understood what you need correctly, but is it something like this?

from typing import Dict


dictionary = {
    "key1": [1, 2, 3, 4, 4, 1],
    "key2": [1, 2, 2, 2, 1, 5],
    "key3": [100, 3, 100, 9],
}

occurrences_dict: Dict[str, Dict[int, int]] = {key: {} for key in dictionary}

for key, numbers_list in dictionary.items():
    for number in numbers_list:
        occurrences = numbers_list.count(number)
        occurrences_dict[key].update({number: occurrences})


print(occurrences_dict)

The output is the following:

{
    "key1": {1: 2, 2: 1, 3: 1, 4: 2},
    "key2": {1: 2, 2: 3, 5: 1},
    "key3": {100: 2, 3: 1, 9: 1},
}

You get a dictionary with the same keys as the original and within each key, you have a dictionary with the number of occurrences of each number on the corresponding list

I don't know if you want sum the values of each dictionary or sum them all, but I will try to implement and you adapt to your specific use.

Using list compreenssion is possible decompose the items:

d = {'key1': [1, 2, 3, 4, 4, 1], 'key2': [1, 2, 2, 2, 1, 5], 'key3': [100, 3, 100, 9]}

w = [(a,x,b.count(x)) for a,b in d.items() for x in set(b)]
# w = [('key1', 1, 2), ('key1', 2, 1), ('key1', 3, 1), ('key1', 4, 2), ('key2', 1, 2), 
#     ('key2', 2, 3), ('key2', 5, 1), ('key3', 9, 1), ('key3', 3, 1), ('key3', 100, 2)]

Then iterate:

d = dict()
for key,i,value in w:
    d[i] = value if i not in d else d[i]+value
# d = {1: 4, 2: 4, 3: 2, 4: 2, 5: 1, 9: 1, 100: 2}

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