• Home
  • Django
  • make the multi dimensional array unique row

make the multi dimensional array unique row

linkaiyi
Follow

make unique rows for multi dimensional array

Use numpy unique

import numpy as np

data = np.array([[1,8,3,3,4],
                 [1,8,9,9,4],
                 [1,8,3,3,4]])
>>> np.unique(data) # flat the array and generate uniqueness of the elements
array([1, 3, 4, 8, 9])
new_array = [tuple(row) for row in data]
uniques = np.unique(new_array, axis=0) # important the axis parameter
>>> uniques
array([[1, 8, 3, 3, 4],
       [1, 8, 9, 9, 4]])

use python set function

data = [(1,8,3,3,4),
        (1,8,9,9,4),
        (1,8,3,3,4)]
uniques = list(set(data))
>>> uniques
array([[1, 8, 3, 3, 4],
       [1, 8, 9, 9, 4]])
Object has 0 attachments

Was this article helpful?

This article is viewed 7 times!

Recent Viewed Articles

0 Comments

Leave a Comment

Support