-1

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]
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Klimt865
  • 83
  • 5

3 Answers3

0

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]

Andrew
  • 176
  • 1
  • 14
0
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}
Bhaskar Gupta
  • 109
  • 1
  • 7
  • Your A and OP's A don't match. – user47 Aug 27 '22 at 12:05
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Aug 28 '22 at 11:50
0

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)
Mehmaam
  • 573
  • 7
  • 22