I am currently learning python and came upon a problem.
I need to know if a element is part of a list, basically, the frequency of occurrence inside the list:
For example, Suppose I have this list of lists:
A = [[1, 2, 4, 5], [2, 3, 5], [1, 2, 4, 5],
[1, 2, 3, 5], [1, 2, 3, 4, 5], [2, 3, 4]]
and let's suppose I need to know if the following numbers appear on the list: [1, 2, 4]
As you may see, the numbers [1, 2, 4] appear on the list, on positions A[0], A[2], and A[4] (the order does not matter, just if the three of them appear at the same time). So, the frequency of appearance should be 3.
I've tried to use any() to see if there was a way to check if x was part of list A
any(x == [1,2,4] for x in A)
Or use for loops, but they only check if each individual of x is part of A, but I need to know whether the a certain series of elements appear.
for i in range(len(x)):
count=0
print("i",i)
for j in range(len(x[i])):
print("j",j)
for k in range(len(A)):
print("k",k)
for l in range(len(A[k])):
if x[i][j]==A[k][l]:
count+=1
Is there a more efficient way to check this in Python?