Python Numpy中数组的集合操作详解
作者:古明地觉 时间:2023-12-04 12:02:24
我们知道两个 set 对象之间,可以取交集、并集、差集、对称差集,举个例子:
s1 = {1, 2, 3}
s2 = {2, 3, 4}
"""
&: 交集
|: 并集
-: 差集
^: 对称差集
"""
# 以下几种方式是等价的
# 但是一般我们都会使用操作符来进行处理,因为比较方便
print(s1 & s1)
print(s1.intersection(s2))
print(set.intersection(s1, s2))
"""
{2, 3}
{2, 3}
{2, 3}
"""
print(s1 | s2)
print(s1.union(s2))
print(set.union(s1, s1))
"""
{1, 2, 3, 4}
{1, 2, 3, 4}
{1, 2, 3, 4}
"""
print(s1 - s2, s2 - s1)
print(s1.difference(s2),
s2.difference(s1))
print(set.difference(s1, s2),
set.difference(s2, s1))
"""
{1} {4}
{1} {4}
{1} {4}
"""
print(s1 ^ s2)
print(s1.symmetric_difference(s2))
print(set.symmetric_difference(s1, s2))
"""
{1, 4}
{1, 4}
{1, 4}
"""
# 另外,我们还可以同时对多个集合操作,不仅仅是两个
print({1, 2, 3} & {2, 3, 4} & {3, 4, 5}) # {3}
那么 Numpy 的数组之间,可不可以执行这些操作呢?答案是可以的,Numpy 提供了一些 API,用于数组之间的集合运算。
但需要注意,数组虽然也支持 & 等操作符,但是它们代表的意义和集合无关。
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([2, 3, 4])
# 两个数组 &
# 表示将数组里面对应的元素分别进行"按位与"操作
print(arr1 & arr2) # [0 2 0]
所以我们需要使用 Numpy 提供的 API 进行运算。
import numpy as np
arr1 = np.array([1, 2, 2, 3])
arr2 = np.array([2, 3, 4, 4])
# 取交集
print(
np.intersect1d(arr1, arr2)
) # [2 3]
# 取并集
print(
np.union1d(arr1, arr2)
) # [1 2 3 4]
# 取差集
print(
np.setdiff1d(arr1, arr2),
np.setdiff1d(arr2, arr1)
) # [1] [4]
# 取对称差集
print(
np.setxor1d(arr1, arr2)
) # [1 4]
接收两个数组,返回一个数组。并且原始数组中的元素允许重复,对结果没有影响。
另外,上面的函数都只能接收两个数组,如果我们想同时对任意多个数组操作呢?很简单,使用 reduce 即可。
from functools import reduce
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([2, 3, 4])
arr3 = np.array([3, 4, 5])
print(
reduce(np.intersect1d,
[arr1, arr2, arr3])
) # [3]
总的来说还是比较简单的。
来源:https://mp.weixin.qq.com/s/DCet9vqT_aV-N2LIIQ_9Og
标签:Python,Numpy,数组,集合
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python扫描IP段查看指定端口是否开放的方法
2023-10-14 22:57:17
sql无效字符 执行sql语句报错解决方案
2023-06-24 19:47:45
基于Python制作一个汇率换算程序
2022-05-25 20:33:25
Mysql数据库百万级数据测试索引效果
2024-01-24 01:30:03
![](https://img.aspxhome.com/file/2023/4/108904_0s.png)
判断li是否有样式的js代码
2024-04-30 09:59:29
Python获取任意xml节点值的方法
2023-03-09 01:40:07
pyqt5对用qt designer设计的窗体实现弹出子窗口的示例
2023-01-28 12:58:07
node.js实现微信开发之获取用户授权
2024-05-03 15:54:04
python实现使用遗传算法进行图片拟合
2022-05-25 03:31:53
![](https://img.aspxhome.com/file/2023/5/124795_0s.png)
Python爬取12306车次信息代码详解
2022-05-19 03:42:00
![](https://img.aspxhome.com/file/2023/3/103333_0s.png)
Python趣味挑战之turtle库绘画飘落的银杏树
2023-07-21 21:46:40
![](https://img.aspxhome.com/file/2023/2/86372_0s.png)
Python将文字转成语音并读出来的实例详解
2021-11-08 21:23:34
![](https://img.aspxhome.com/file/2023/3/131443_0s.jpg)
PyQt5中QTimer定时器的实例代码
2021-06-01 07:28:54
![](https://img.aspxhome.com/file/2023/7/93797_0s.png)
python matlibplot绘制3D图形
2023-05-21 18:47:36
![](https://img.aspxhome.com/file/2023/4/87724_0s.png)
详解Python实现进度条的4种方式
2023-07-05 13:31:16
![](https://img.aspxhome.com/file/2023/4/134814_0s.gif)
无法在发生错误时创建会话,请检查 PHP 或网站服务器日志,并正确配置 PHP 安装最快的解决办法
2023-09-03 19:49:50
JS遮罩层效果 兼容ie firefox jQuery遮罩层
2024-02-24 23:04:24
python获取时间及时间格式转换问题实例代码详解
2021-12-20 12:46:20
centos系统升级python 2.7.3
2023-08-18 06:40:52
Python破解网站登录密码脚本
2022-09-29 23:21:38
![](https://img.aspxhome.com/file/2023/8/93458_0s.png)