How can I find repeating lists and the number of times they repeat?
For example:
#fsa and ghf are both lists
kol = []
for i in range(len(fsa)):
if fsa[i] < ghf[i]:
kol.append('1')
else:
kol.append('2')
# this code puts my data into multiple lists in six intervals meaning six
# elements of data per list
start = 0
end = len(fsa)
for x in range(start,end,6):
t = kol[x:x 6]
l = (fsa[x:x 6])
m = ghf[x:x 6]
if m[0] < m[-1]:
print(t)
else:
print(t)
Output:
['1', '1', '1', '1', '1', '1']
['2', '2', '2', '2', '2', '2']
['1', '1', '1', '1', '1', '1']
['1', '1', '1', '1', '1', '1']
['2', '2', '2', '2', '2', '2']
What I want to find out which lists repeat themselves and how many times each repetition occurs.
Example of what I want:
['1', '1', '1', '1', '1', '1'] 3 # the number of times it repeats
['2', '2', '2', '2', '2', '2'] 2
CodePudding user response:
You could use the collections.Counter
dictionary subclass to find out what you want. To use it you will need to convert each list into a tuple to make it compatible (because the items being counted must be hashable).
from collections import Counter
lists = [['1', '1', '1', '1', '1', '1'],
['2', '2', '2', '2', '2', '2'],
['1', '1', '1', '1', '1', '1'],
['1', '1', '1', '1', '1', '1'],
['2', '2', '2', '2', '2', '2']]
counter = Counter(tuple(lst) for lst in lists)
for members, count in counter.items():
print(list(members), count)
Output:
['1', '1', '1', '1', '1', '1'] 3
['2', '2', '2', '2', '2', '2'] 2