简体   繁体   中英

Python - Compare list element if exist

I have a large table couple of millions of pairs of ints [[1,2],[45,101],[22,222] etc..]. What is the quickest way in Python to remove duplicates ?

Creating empty list and appending it "if not in" doesn't work since it takes ages. Converting to Numpy and use "isin" I can't seem to get it to work on pairs.

you can do the following

arr = [[1,2],[45,101],[22,222], [1,2]]

arr = set(tuple(i) for i in arr)

if you want to convert it back to list

arr = [list(i) for i in arr]

Probably going to be this: list(set(my_list))

Edit: Whoops. In any case, if whatever is iterating over said list can perform the task of detecting duplicates, that'd be the faster than removing duplicates beforehand.

You could use np.unique() :

np.unique([[1,2],[45,101],[22,222],[22,222]], axis=0)

Output:

array([[  1,   2],
       [ 22, 222],
       [ 45, 101]])

Note that this re-orders the list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM