my code is this
idnt=[]
idntfrq=[]
for i in range(len(occ)):
idnt.append([])
idntfrq.append([])
for j in range(len(occ[i])):
for j2 in range(j,len(occ[i])):
for d in occ[i][j]:
idnt[i].append(d)
idntfrq[i].append([j])
occ[i][j].remove(d)
for d2 in occ[i][j2]:
if d==d2:
idntfrq[i][-1].append(j2)
occ[i][j2].remove(d)
the list of lists is occ (50 lists inside with various lengths each)
the thought was to iterate over everything,store each value in the idnt[i] list and the index of the list in which it appears to the idntfrq[i] list and then remove the ellement from the list of the current itteration,the occ list should be empty after that but it is not,i uploaded a prntscr of the occ[0][0] to see what i mean
NOTE:each list inside a list contains every element only once , but i want to count the occurences across all the lists inside every occ[i] (for i in 50)and also keep the indexenter image description here
CodePudding user response:
This code will do what you are asking:
mainList = [[1,2,3],[3,4,5],[1,5,9]]
d = {}
for l in mainList:
for item in l:
if item in d.keys():
d[item] =1
else:
d[item] = 1
print(d)
Output:
{1: 2, 2: 1, 3: 2, 4: 1, 5: 2, 9: 1}
It gives the answer in a dictionary where keys are the items and the values is the number of appearances.
This output and be further formatted if needed.
CodePudding user response:
I think this works:
from collections import Counter
list_of_lists = [[1, 2, 3], [4, 1, 2], [3, 4, 5]]
counter = Counter()
for _list in list_of_lists:
counter.update(_list)