Related to Counter when 2 values are most frequent
I made a demo list for the example, Running it with “for i in range (len(lines))”:
from statistics import multimode lines = [[0,1],[1,5],[1,3],[67,45],[98,23],[67,68],[23,2],[1,18],[23,67],[40,79],[40,234],[40,41],[41,51]] most = multimode(item for sublist in lines for item in sublist) for a in most: del_connected = [bin for bin in lines if a in bin] # these connected to the maximum occured for i, k in del_connected: lines = [x for x in lines if k not in x] lines = [x for x in lines if i not in x] print(lines)
First round, there is only one occurred value “1”, but in the second round there are 3: 41,23,67. That’s why I did a for loop and matched “most” to “a” but del_connected prints the wrong values and so the “lines” list itself –>
>>[[67, 45], [67, 68], [23, 67]] >>[] >>[[40, 79], [40, 234], [40, 41]]
How do I fix it’s print when there are more than one frequent value?
Advertisement
Answer
Do you want something like this:
lines = ... while len(lines) > 0: print(lines) most = multimode(item for sublist in lines for item in sublist) connected = [ bin for bin in lines for a in most if a in bin ] for i, k in connected: lines = [ bin for bin in lines if (i not in bin) or (k not in bin) ]