利用python查看数组中的所有元素是否相同


Posted in Python onJanuary 08, 2021

不知道大家有没有过这种经历,就是想要判断两个数组运算后得到的新数组中的各个元素值是否相同。这里给出一种使用np.unique()的方法,代码如下:

import numpy as np


class Debug:
 @staticmethod
 def isAllElementSame():
 x1 = np.array([[1, 2, 3], [3, 4, 5], [6, 7, 8]])
 x2 = np.array([[81., 162., 243., ], [243., 324., 405.], [486., 567., 648.]])
 print('The result if x2/x1 is:')
 print(x2 / x1)
 print('Judge whether all elements in array are same or not')
 print(len(np.unique(x2 / x1)) == 1)


if __name__ == '__main__':
 debug = Debug()
 debug.isAllElementSame()
"""
The result if x2/x1 is:
[[81. 81. 81.]
 [81. 81. 81.]
 [81. 81. 81.]]
Judge whether all elements in array are same or not
True
"""

可以看到,当输出为True的时候,表明数组中的所有元素的值均一致,反之,当为False的时候,数组中存在不一样的元素值。

如果数组中的元素是复数呢?

import numpy as np


class Debug:
 @staticmethod
 def isAllElementSame():
  x1 = np.array([complex(1, 2), complex(2, 4)])
  x2 = np.array([complex(2, 4), complex(4, 8)])
  print('The result if x2/x1 is:')
  print(x2 / x1)
  print('Judge whether all elements in array are same or not')
  print(len(np.unique(x2 / x1)) == 1)


if __name__ == '__main__':
 debug = Debug()
 debug.isAllElementSame()
"""
The result if x2/x1 is:
[2.+0.j 2.+0.j]
Judge whether all elements in array are same or not
True
"""

可以看到,当数组元素为复数时,该方法仍然适用。然而当数组元素为小数时,可能会失效,如果失效,加上np.round()函数并设定所需要保留的有效位小数即可,例如:print(len(np.unique(np.round(x2 / x1))) == 1)。

到此这篇关于利用python查看数组中的所有元素是否相同的文章就介绍到这了,更多相关python查看数组元素相同内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python网络爬虫学习笔记(1)
Apr 09 Python
python编程使用协程并发的优缺点
Sep 20 Python
flask-restful使用总结
Dec 04 Python
Python获取网段内ping通IP的方法
Jan 31 Python
Python3.6实现带有简单界面的有道翻译小程序
Apr 16 Python
Python3中列表list合并的四种方法
Apr 19 Python
python3.8 微信发送服务器监控报警消息代码实现
Nov 05 Python
Pytorch 神经网络—自定义数据集上实现教程
Jan 07 Python
如何基于pythonnet调用halcon脚本
Jan 20 Python
在ipython notebook中使用argparse方式
Apr 20 Python
python 远程执行命令的详细代码
Feb 15 Python
python通过新建环境安装tfx的问题
May 20 Python
Python爬虫自动化获取华图和粉笔网站的错题(推荐)
Jan 08 #Python
tensorflow与numpy的版本兼容性问题的解决
Jan 08 #Python
matplotlib自定义鼠标光标坐标格式的实现
Jan 08 #Python
selenium设置浏览器为headless无头模式(Chrome和Firefox)
Jan 08 #Python
python画图时设置分辨率和画布大小的实现(plt.figure())
Jan 08 #Python
python使用matplotlib的savefig保存时图片保存不完整的问题
Jan 08 #Python
Numpy中的数组搜索中np.where方法详细介绍
Jan 08 #Python
You might like
生成缩略图
2006/10/09 PHP
解析php入库和出库
2013/06/25 PHP
解析WordPress中函数钩子hook的作用及基本用法
2015/12/22 PHP
PHP实现负载均衡的加权轮询方法分析
2018/08/22 PHP
深入理解JavaScript系列(14) 作用域链介绍(Scope Chain)
2012/04/12 Javascript
jquery实现输入框动态增减的实例代码
2013/07/14 Javascript
禁止页面刷新让F5快捷键及右键都无效
2014/01/22 Javascript
JavaScript使用指针操作实现约瑟夫问题实例
2015/04/07 Javascript
jquery+CSS3实现3D拖拽相册效果
2016/07/18 Javascript
用jmSlip编写移动端顶部日历选择控件
2016/10/24 Javascript
AngularJS改变元素显示状态
2017/04/20 Javascript
使用 Vue.js 仿百度搜索框的实例代码
2017/05/09 Javascript
Angular 2父子组件之间共享服务通信的实现
2017/07/04 Javascript
NodeJS收发GET和POST请求的示例代码
2017/08/25 NodeJs
JS去掉字符串末尾的标点符号及删除最后一个字符的方法
2017/10/24 Javascript
使用Vue.js和Element-UI做一个简单登录页面的实例
2018/02/23 Javascript
vue2.0父子组件间传递数据的方法
2018/08/16 Javascript
python复制文件代码实现
2013/12/23 Python
基于asyncio 异步协程框架实现收集B站直播弹幕
2016/09/11 Python
python编写弹球游戏的实现代码
2018/03/12 Python
Django实现单用户登录的方法示例
2019/03/28 Python
python进程的状态、创建及使用方法详解
2019/12/06 Python
python实现登录与注册系统
2020/11/30 Python
python爬取youtube视频的示例代码
2021/03/03 Python
Joseph官网:英国小众奢侈品牌
2019/05/17 全球购物
node中使用shell脚本的方法步骤
2021/03/23 Javascript
护士毕业生自我鉴定
2014/02/08 职场文书
学校消防演习方案
2014/02/19 职场文书
社区平安建设方案
2014/05/25 职场文书
房地产资料员岗位职责
2014/07/02 职场文书
优秀教师单行材料
2014/12/16 职场文书
写给医生的感谢信
2015/01/22 职场文书
2015年社会实践个人总结
2015/03/06 职场文书
公积金具结保证书
2015/05/11 职场文书
python图片灰度化处理的几种方法
2021/06/23 Python
vue报错function () { [native code] },无法出现我们想要的内容 Unknown custom element
2022/04/11 Vue.js