-1

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)))
Community
  • 1
  • 1
  • You are calling the function for each individual element. It is just printing the element, then an empty list, since it is not possible to have a duplicate with only one element. – Rob Foley Jul 31 '15 at 11:23
  • Try printing `XORcomb` maybe it no longer has `1` or `2` or so. – Anand S Kumar Jul 31 '15 at 11:23
  • I get an entirely other result if I use the list above in `XORComb`, could you check that `XORComb` is what you think it is? – skyking Jul 31 '15 at 11:29
  • 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] – Vinod Ranganath Jul 31 '15 at 11:42
  • XORcomb hasn't changed – Vinod Ranganath Jul 31 '15 at 11:43
  • can you show the complete code you are using? How is XORcomb defined? – Anand S Kumar Jul 31 '15 at 11:54
  • Anand S Kumar, I just edited my post. You'll find the code there (the complete code is too long, so only the relevant lines for this question) – Vinod Ranganath Jul 31 '15 at 12:15
  • My output is exactly the same as elegent's. If you want an answer please provide an example (including output/input) that fails that we can reproduce. – skyking Jul 31 '15 at 12:25
  • the example is given in the final code above. Try giving no. of values as 5, and the elements as 1, 2, 3, 4, 5. I need to search for the elements input to N (user-defined). – Vinod Ranganath Jul 31 '15 at 12:41
  • @skyking it works. check the comments below.....thank you – Vinod Ranganath Jul 31 '15 at 12:48
  • I still get the same result as elegent below (if that's is not the result that you'd expect you'll have to explain why). If your code on the other hand results in the expected result - I don't really understand what the question is. – skyking Jul 31 '15 at 12:56
  • if you see above, i didn't convert elements into integer before appending to list N.....which resulted in not producing an output. That's the problem. – Vinod Ranganath Aug 01 '15 at 04:26

1 Answers1

1

The problem is that input() returns a string not a number. When you run your duplicates() function you compare integers and strings and get an empty result, because there are no stringgs in your XORcomb list.

You need to convert it using int() before adding it to N as you used did for n:

>>> XORcomb = [None, None, None, None, None, None, 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]
>>> N = []
>>> n = int(input("Enter number of values: "))

>>> for e in range(0, n):
       element = input("Enter element {}: ".format(str(e + 1)))
       N.append(int(element))


>>> for each in N:
     print("%r occurs at: %r " %(each, duplicates(XORcomb, each)))


1 occurs at: [16, 21, 24, 25] 
2 occurs at: [6, 29, 30] 
3 occurs at: [7, 10, 28] 
4 occurs at: [8, 11, 13, 27] 
5 occurs at: [9, 12, 14, 15, 26] 
elegent
  • 3,857
  • 3
  • 23
  • 36