Home > OS >  How can I get the distinct value inside 2D or any nD array using Python 3?
How can I get the distinct value inside 2D or any nD array using Python 3?

Time:12-07

I'm trying to get the distinct value(s) inside a 2D or nD array and then print some message when it saw one or more.

arr = [[1,1,3],[2,4,2]]

Distinct values inside array: 3, 4

Duplicated values inside array: 1, 2
1 appeared 2 times
2 appeared 2 times

I tried this initial code but no luck (incomplete code):

import numpy as np

R = int(input("Enter the number of rows: "))
C = int(input("Enter the number of columns: "))
  
matrix = []
print("Enter the numbers:")

for i in range(R):         
    a =[]
    for j in range(C):     
         a.append(int(input()))
    matrix.append(a)

arr = np.array(matrix).flatten()


def areDistinct(arr) :
    n = len(arr)
    s = set()
    for i in range(0, n):
        s.add(arr[i])
    return (len(s) == len(arr))

 
if (areDistinct(arr)):
    print("DISTINCT")
else :
    print("DUPLICATED")

CodePudding user response:

As you tagged your question with Numpy tag, I suppose that you are looking for a Numpythonic solution (in contrast to plain Pytnon).

So start with the necessary import:

import numpy as np

I assume that you created your arr something like:

arr = np.array([[1,1,3],[2,4,2]])

First compute unique values and their respective counts (how many times each value occurs):

vals, cnts = np.unique(arr.flatten(), return_counts=True)

The second step is to compute uniqueness indicators:

uniq = cnts == 1

For your data sample the results are:

array([1, 2, 3, 4])
array([2, 2, 1, 1], dtype=int64)
array([False, False,  True,  True])

Then, to generate your expected printout, run:

print(f'Distinct values inside array: {", ".join(map(str, vals[uniq]))}')
print(f'Duplicated values inside array: {", ".join(map(str, vals[~uniq]))}')
for i in [ind for ind, x in enumerate(uniq) if ~x]:
    print(f'{vals[i]} appeared {cnts[i]} times')

No need to import any other packages like collections or itertools.

CodePudding user response:

You can try flattening the array and then putting it through the built-in Counter

flattened = flatten(arr) counts_of_values = Counter(flattened)

and then just filter based on the counts, so like this:

from itertools import chain
from collections import Counter
flattened = list(chain(*arr))
c = Counter(flattened)
distinct = [k for k, v in c.items() if v < 2]
duplicated = [k for k, v in c.items() if v >= 2]

CodePudding user response:

values=dict()
arr=[[1,1,3,4,6,6,7,4,3,2,5,7],[3,2,1]]
for i in arr:
    for j in i:
        if j in values:
            values[j] =1
        else:
            values[j]=1
distinct = []
duplicate = dict()
for i in values:
    if values[i] == 1:
        distinct.append(i)
    else:
        duplicate[i] = values[i]
print(distinct)
print(duplicate)

CodePudding user response:

arr = [[1,1,3],[2,4,2]]

res = dict()

# compute histogram
for inner_arr in arr:
    for a in inner_arr:
        if a in res:
            res[a]  = 1
        else:
            res[a] = 1

# scan histogram and check for distinct values
for r in res:
    if res[r] == 1:
        print(f"The value {r} appeared once.")
    else:
        print(f"The value {r} appeared {res[r]} times.")

you can easily scan res and return all keys that has associated values 1. You can see res dictionary has a histogram.

  • Related