I saw this solution in another post Index of duplicates items in a python list , where it is made clear that using the below code, you can get the index positions of repeating elements in a list.
[[], [], [], [], [], [], 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 1, 6, 7, 7, 6, 1, 7, 6, 1, 1, 5, 4, 3, 2, 2, 0]
def duplicates(lst, item):
return [j for j, x in enumerate(lst) if x == item]
print(duplicates(XORcomb, 2)))
OUTPUT
[6, 29, 30]
However, if i try to iterate the process, it doesn't work.
N = [1, 2, 3, 4, 5]
def duplicates(lst, item):
return [i for i, x in enumerate(lst) if x == item]
for each in N:
print("%r occurs at: %r " %(each, duplicates(XORcomb, each)))
OUTPUT
'1' occurs at: []
'2' occurs at: []
'3' occurs at: []
'4' occurs at: []
'5' occurs at: []
It works fine if list N is pre-defined. But if list N is user-defined, then it doesn't work.
n = int(input("Enter number of values: ")) #Get user input for no. of values
N = []
for e in range (1, n+1):
elements = input("Enter element %r: " %e)
N.append(elements)
XORcomb = [[], [], [], [], [], [], 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 1, 6, 7, 7, 6, 1, 7, 6, 1, 1, 5, 4, 3, 2, 2, 0]
def duplicates(lst, item):
return [j for j, x in enumerate(lst) if x == item]
for each in N:
print("%r occurs at: %r " %(each, duplicates(XORcomb, each)))