Grab unique tuples in python list, irrespective of order

后端 未结 4 1747
予麋鹿
予麋鹿 2021-01-04 10:14

I have a python list:

[ (2,2),(2,3),(1,4),(2,2), etc...]

What I need is some kind of function that reduces it to its unique components... w

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-04 10:47

    you could simply do

    y = np.unique(x, axis=0)
    z = [] 
    for i in y:
       z.append(tuple(i))
    

    The reason is that a list of tuples is interpreted by numpy as a 2D array. By setting axis=0, you'd be asking numpy not to flatten the array and return unique rows.

提交回复
热议问题