-1

I am trying to find duplicates inside an array. my array looks like:

['LM_Auto', 'LM_Auto', 'LM_Op', 'LM_Op'] 

and much longer with a few thousand of these pairs.

def clear_stemmed_LM(collection): 
i = 0
ele = i+1
for i in collection:
    for ele in collection:
        if i == ele:
            del collection[ele]
return collection 

the error says the list indices must be integers or slices not strings. How can I compare strings in an array?

[if x+1 for in x range] #Idea

or something similar like this?

Yannisch
  • 7
  • 4

2 Answers2

1

If you just need to purge the duplicates in the list collection, as it seems from the code, list(set(collection)).

This is a very, very common and basic issue. I encourage you to Google thoroughly first!

Lester Jack
  • 179
  • 8
0

for i in collection iters over the items so in i you'll find the first string not the index. Use for i in range(len(collection)) instead

User
  • 806
  • 1
  • 11
  • 28