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))”:
JavaScript
x
11
11
1
from statistics import multimode
2
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]]
3
4
most = multimode(item for sublist in lines for item in sublist)
5
for a in most:
6
del_connected = [bin for bin in lines if a in bin] # these connected to the maximum occured
7
for i, k in del_connected:
8
lines = [x for x in lines if k not in x]
9
lines = [x for x in lines if i not in x]
10
print(lines)
11
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 –>
JavaScript
1
4
1
>>[[67, 45], [67, 68], [23, 67]]
2
>>[]
3
>>[[40, 79], [40, 234], [40, 41]]
4
How do I fix it’s print when there are more than one frequent value?
Advertisement
Answer
Do you want something like this:
JavaScript
1
18
18
1
lines =
2
3
while len(lines) > 0:
4
print(lines)
5
most = multimode(item for sublist in lines for item in sublist)
6
connected = [
7
bin
8
for bin in lines
9
for a in most
10
if a in bin
11
]
12
for i, k in connected:
13
lines = [
14
bin
15
for bin in lines
16
if (i not in bin) or (k not in bin)
17
]
18