简体   繁体   中英

length of numpy.unique on a 2D list of shape (1,*)

When calling numpy.unique on a list of shape (1, X) , for instance a = [[1,2]] , len(numpy.unique(a)) returns X rather than, as i'd have expected, 1. Generally speaking, numpy.unique seems to treat a as 1D array..

In contrast to this, len(numpy.array(a)) does return 1.

What is the motivation behind this?

EDIT: python version 3.7.3

The answer you get is because the array is flattened. And the length of [1,2] is indeed 2. I believe the reason this is done is just to give the maximum amount of flexibility to the user.

What you were looking for can be achieved by doing this instead:

len(np.unique(a, axis=0))

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