利用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 相关文章推荐
用yum安装MySQLdb模块的步骤方法
Dec 15 Python
JPype实现在python中调用JAVA的实例
Jul 19 Python
python中的文件打开与关闭操作命令介绍
Apr 26 Python
Python找出微信上删除你好友的人脚本写法
Nov 01 Python
对python的bytes类型数据split分割切片方法
Dec 04 Python
对python dataframe逻辑取值的方法详解
Jan 30 Python
python logging模块的使用总结
Jul 09 Python
Python+AutoIt实现界面工具开发过程详解
Aug 07 Python
python模拟预测一下新型冠状病毒肺炎的数据
Feb 01 Python
python yield和Generator函数用法详解
Feb 10 Python
使用python创建Excel工作簿及工作表过程图解
May 27 Python
Python字符串及文本模式方法详解
Sep 10 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+mysql留言本源码
2009/11/11 PHP
PHP编码规范之注释和文件结构说明
2010/07/09 PHP
PHP加密扩展库Mcrypt安装和实例
2013/11/10 PHP
PHP集成百度Ueditor 1.4.3
2014/11/23 PHP
PHP单例模式详解及实例代码
2016/12/21 PHP
PHP面向对象之事务脚本模式(详解)
2017/06/07 PHP
php中get_object_vars()在数组的实例用法
2021/02/22 PHP
javascript下function声明一些小结
2007/12/28 Javascript
对于this和$(this)的个人理解
2013/09/08 Javascript
JavaScript fontcolor方法入门实例(按照指定的颜色来显示字符串)
2014/10/17 Javascript
在 Angular2 中实现自定义校验指令(确认密码)的方法
2017/01/23 Javascript
JavaScript实现公历转农历功能示例
2017/02/13 Javascript
关于vue.extend和vue.component的区别浅析
2017/08/16 Javascript
原生javascript实现的全屏滚动功能示例
2017/09/19 Javascript
vue自定义移动端touch事件之点击、滑动、长按事件
2018/07/10 Javascript
vue中组件的过渡动画及实现代码
2018/11/21 Javascript
Nodejs对postgresql基本操作的封装方法
2019/02/20 NodeJs
详解微信UnionID作用
2019/05/15 Javascript
Javascript 对象(object)合并操作实例分析
2019/07/30 Javascript
jQuery实现轮播图效果
2019/11/26 jQuery
利用原生JS实现欢乐水果机小游戏
2020/04/23 Javascript
python实现简单的socket server实例
2015/04/29 Python
caffe binaryproto 与 npy相互转换的实例讲解
2018/07/09 Python
Python Pandas对缺失值的处理方法
2019/09/27 Python
python压包的概念及实例详解
2021/02/17 Python
HTML5 canvas基本绘图之绘制矩形
2016/06/27 HTML / CSS
英国翻新电子产品购物网站:Tech Trade
2017/12/25 全球购物
Why we need EJB
2016/10/20 面试题
优秀员工自荐书
2013/12/19 职场文书
2014年两会学习心得范例
2014/03/17 职场文书
债务纠纷委托书
2014/08/30 职场文书
2014年国庆节演讲稿
2014/09/02 职场文书
银行安全保卫工作总结
2015/08/10 职场文书
2016年寒假社会实践活动总结
2015/10/10 职场文书
信息技术研修心得体会
2016/01/08 职场文书
课文《燕子》教学反思
2016/02/17 职场文书