Average of MultiMode of a list of a list


Joined
Apr 7, 2022
Messages
9
Reaction score
0
my_list = [[R1,G1,B1],[R2,G2,B2],...] is a list of red, green, blue value of a pixels. Each pixel has an R,G,B value.

I want to find the mode of the most common RGB value.
If there are multiple modes, I then need the average R, average G, average B

I feel like if I give some examples It'll explain what I am after:

my_list = [[1,2,3],[4,5,6],[1,8,9],[1,2,3],[1,8,9]]

The modes of my_list would be: [1,2,3],[1,8,9]

Average R (0th terms) = (1+1)/2=1
Average G (1st terms) = (2+8)/2=5
Average B (2nd terms) = (3+9)/2=6

Below is a similar question however the order of the sub-lists can get mixed up, and I also dont know how to get the average of all the 0th terms (R), average of all 1st terms (G), average of all the 2nd terms (B):

Any ideas?
 
Ad

Advertisements

Joined
May 11, 2022
Messages
58
Reaction score
5
so yeah fairly simple.
Code:
RGB ={}
for i in range(0, len(my_list)):
   if my_list[i] in RGB:
      RGB[i] += 1
   else:
      RGB += {my_list[i]: 1}
maxmode = [[-1,-1,-1],0]
for i in range(0,len(RGB)):
    if RGB[i] > maxmode[0][1]:
      maxmode = [RGB[i].key,RGB[i]]
    if RGB[i] == maxmode[-1][1]:
       maxmode +=[RGB[i].key,RGB[i]]
if len(maxmode) == 2:
   print(maxmode[0][0])
else:
   r,g,b = 0,0,0
   for i in range(0,len(maxmode),2):
      r += maxmode[i][0][0]
      g += maxmode[i][0][1]
      b += maxmode[i][0][2]
r = r/(len(maxmode)/2)
g = g/(len(maxmode)/2)
b = b/(len(maxmode)/2)
print(r,g,b)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top