利用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回调函数的使用方法
Jan 23 Python
基于Python实现的百度贴吧网络爬虫实例
Apr 17 Python
详解Python操作RabbitMQ服务器消息队列的远程结果返回
Jun 30 Python
Python sorted函数详解(高级篇)
Sep 18 Python
在python image 中安装中文字体的实现方法
Aug 22 Python
python程序 线程队列queue使用方法解析
Sep 23 Python
python自动化unittest yaml使用过程解析
Feb 03 Python
Python求两个字符串最长公共子序列代码实例
Mar 05 Python
简单了解Python字典copy与赋值的区别
Sep 16 Python
浅析Python requests 模块
Oct 09 Python
python 实现socket服务端并发的四种方式
Dec 14 Python
python基础之类方法和静态方法
Oct 24 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
php 获取百度的热词数据的代码
2012/02/18 PHP
Zend studio文件注释模板设置方法
2013/09/29 PHP
PHP中常用的字符串格式化函数总结
2014/11/19 PHP
PHP获取二维数组中某一列的值集合
2015/12/25 PHP
Extjs学习笔记之八 继承和事件基础
2010/01/08 Javascript
jquery 插件学习(三)
2012/08/06 Javascript
jQuery中实现动画效果的基本操作介绍
2013/04/16 Javascript
setTimeout()与setInterval()方法区别介绍
2013/12/24 Javascript
JQuery解析HTML、JSON和XML实例详解
2014/03/29 Javascript
轻松实现javascript图片轮播特效
2016/01/13 Javascript
Bootstrap Modal遮罩弹出层代码分享
2016/11/21 Javascript
Angular.js实现动态加载组件详解
2017/05/28 Javascript
浅谈JavaScript作用域和闭包
2017/09/18 Javascript
详解vue.js之props传递参数
2017/12/12 Javascript
原生实现一个react-redux的代码示例
2018/06/08 Javascript
vue 地区选择器v-distpicker的常用功能
2019/07/23 Javascript
[01:08:29]DOTA2-DPC中国联赛定级赛 RNG vs Aster BO3第一场 1月9日
2021/03/11 DOTA
python实现的文件同步服务器实例
2015/06/02 Python
python僵尸进程产生的原因
2017/07/21 Python
使用pandas读取csv文件的指定列方法
2018/04/21 Python
Tensorflow 查看变量的值方法
2018/06/14 Python
浅谈django2.0 ForeignKey参数的变化
2019/08/06 Python
Django自带日志 settings.py文件配置方法
2019/08/30 Python
opencv3/Python 稠密光流calcOpticalFlowFarneback详解
2019/12/11 Python
Python网页解析器使用实例详解
2020/05/30 Python
Jmeter调用Python脚本实现参数互相传递的实现
2021/01/22 Python
将SVG图引入到HTML页面的实现
2019/09/20 HTML / CSS
深深扎根运动世界的生活品牌:Tillys
2017/10/30 全球购物
越南综合购物网站:Lazada越南
2019/06/10 全球购物
SQL Server笔试题
2012/01/10 面试题
母亲80寿诞答谢词
2014/01/16 职场文书
优秀幼教自荐信
2014/02/03 职场文书
哈弗商学院毕业生求职信
2014/02/26 职场文书
地震慰问信
2015/02/14 职场文书
创业计划书之酒厂
2019/10/14 职场文书
spring IOC容器的Bean管理XML自动装配过程
2022/05/30 Java/Android