numpy.unique gives wrong output for list of sets(numpy.unique 为集合列表提供错误的输出)
问题描述
我有一个由 给出的集合列表,
I have a list of sets given by,
sets1 = [{1},{2},{1}]
当我使用 numpy 的 unique
在此列表中找到唯一元素时,我得到
When I find the unique elements in this list using numpy's unique
, I get
np.unique(sets1)
Out[18]: array([{1}, {2}, {1}], dtype=object)
可以看出,结果是错误的,因为 {1}
在输出中重复.
As can be seen seen, the result is wrong as {1}
is repeated in the output.
当我通过使相似元素相邻来更改输入中的顺序时,这不会发生.
When I change the order in the input by making similar elements adjacent, this doesn't happen.
sets2 = [{1},{1},{2}]
np.unique(sets2)
Out[21]: array([{1}, {2}], dtype=object)
为什么会发生这种情况?还是我的做法有问题?
Why does this occur? Or is there something wrong in the way I have done?
推荐答案
这里发生的是 np.unique
函数是基于 np._unique1d
函数的NumPy(参见代码这里),其中本身使用 .sort()
方法.
What happens here is that the np.unique
function is based on the np._unique1d
function from NumPy (see the code here), which itself uses the .sort()
method.
现在,对每个集合中仅包含一个整数的集合列表进行排序不会生成一个列表,其中每个集合都按集合中存在的整数值排序.所以我们将拥有(这不是我们想要的):
Now, sorting a list of sets that contain only one integer in each set will not result in a list with each set ordered by the value of the integer present in the set. So we will have (and that is not what we want):
sets = [{1},{2},{1}]
sets.sort()
print(sets)
# > [{1},{2},{1}]
# ie. the list has not been "sorted" like we want it to
现在,正如您所指出的,如果集合列表已经按照您想要的方式排序,则 np.unique
将起作用(因为您会事先对列表进行排序).
Now, as you have pointed out, if the list of sets is already ordered in the way you want, np.unique
will work (since you would have sorted the list beforehand).
一个特定的解决方案(但请注意,它仅适用于每个包含单个整数的集合列表):
One specific solution (though, please be aware that it will only work for a list of sets that each contain a single integer) would then be:
np.unique(sorted(sets, key=lambda x: next(iter(x))))
这篇关于numpy.unique 为集合列表提供错误的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:numpy.unique 为集合列表提供错误的输出


基础教程推荐
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 筛选NumPy数组 2022-01-01