So I have a python dict like:
1:[ "red","blue","green"]
2: ["blue","blue","red"]..
and so on.
and then i have another python dict:
score_dict = {
pid: weight
1: 2
2: 20
...
}
So, what i want is..
in the first dict, count number of times two colors occur together.
and so on.
But that count be multiplied by their weight.
For example:
I want to find out how many times red an blue occured together in this list:
so for pid 1
red and blue occurs once.
so this is (1*2) # 2 comes from the score_dict as pid1 has a weight 2
and then for second
there are two blue, red pairs I can form
so this is (1*20) + (1*20)
So total score for blue and red occuring together is 2 + 20 + 20 = 42
Also, how do i extend it to 3 colors?
Like if i have to find out “red” “blue” and ” green” occuring together?
Result:
The last part of my code could be rewritten as generator comprehension: