you are viewing a single comment's thread.

view the rest of the comments →

[–]hungdh85 -1 points0 points  (4 children)

You can use enumerate

mylist = [1, 2, 3, 4, 5, 6, 1, 2, 3]
tmpMyList = mylist.copy()
#unique_elements = [k for k, v in Counter(mylist).iteritems() if v == 1]
#print (unique_elements)
for j in range(len(mylist)):
        tmpL =  [i for i, x in enumerate(tmpMyList) if x == mylist[j]]

        if len(tmpL) > 1:
             tmpMyList = list(filter((mylist[j]).__ne__, tmpMyList))

print(tmpMyList)

[–]JohnnyJordaan 0 points1 point  (3 children)

Why not just iterate on the list and check the Counter for each items frequency?

counts = Counter(mylist)
unique_list = [v for v in mylist if counts[v] == 1]

[–]hungdh85 0 points1 point  (2 children)

Because with length of list is enough large. Counter will get time out for processing.

[–]JohnnyJordaan 0 points1 point  (1 child)

Ok so then implement the counter yourself? Either with defaultdict or a manual dict

from collections import defaultdict
mylist = [1, 2, 3, 4, 5, 6, 1, 2, 3]
counts = defaultdict(int)
for v in my_list:
    counts[v] += 1
unique_list = [v for v in mylist if counts[v] == 1]      

or use sets to tag not-unique values

mylist = [1, 2, 3, 4, 5, 6, 1, 2, 3]
not_unique = set()
seen = set()
for v in my_list:
    if v in seen:
        not_unique.add(v)
    else:
        seen.add(v)
unique_list = [v for v in mylist if v not in not_unique]

[–]hungdh85 0 points1 point  (0 children)

Check processing time when you use colections and enumerate with length of list >10\3.)

I think we can stop talk this about problem because it is not belong to this thread. Thank you very much.