简体   繁体   中英

Identifying unique index numbers from a list in Python

I have a list A containing an array with indices. I want to print all the unique index numbers by scanning each [i,j] . In [0,3] , i=0,j=3 . I present the expected output.

import numpy as np
A=[np.array([[[0, 1],
        [0, 3],
        [1, 3],
        [3, 4],
        [3, 6],
        [4, 5],
        [4, 7],
        [5, 7],
        [6, 4]]])]

The expected output is

A=[0,1,3,4,5,6,7]

numpy has this very cool function np.unique .

Simply to get the output A=[0,1,3,4,5,6,7]

B = np.unique(A[0])

Output: [0 1 3 4 5 6 7]

A=[[0, 1],[0, 3],[1, 3],[3, 4],[3, 6],[4, 5],[4, 7],[5, 7],[6, 4]]
K = []
for _ in range(len(A)):
    K.extend(A[_])
print(set(K))

OUTPUT:

{0, 1, 3, 4, 5, 6, 7}

A is basically a list and within list you make an array. Do this:

def unique(A):
    x = np.array(A)
    print(np.unique(x))
unique(A)

if A is an array we can simply do this:

np.unique(A)

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