利用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模拟登录百度代码分享(获取百度贴吧等级)
Dec 27 Python
在Python中处理列表之reverse()方法的使用教程
May 21 Python
Python判断字符串与大小写转换
Jun 08 Python
详解Python中的文件操作
Aug 28 Python
python的常用模块之collections模块详解
Dec 06 Python
Python中collections模块的基本使用教程
Dec 07 Python
如何更优雅地写python代码
Jul 02 Python
Python3中urlencode和urldecode的用法详解
Jul 23 Python
Python在cmd上打印彩色文字实现过程详解
Aug 07 Python
pytorch神经网络之卷积层与全连接层参数的设置方法
Aug 18 Python
Tensorflow获取张量Tensor的具体维数实例
Jan 19 Python
2021年值得向Python开发者推荐的VS Code扩展插件
Jan 25 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中抽象类和接口的概念以及区别
2013/06/27 PHP
php数组生成html下拉列表的方法
2015/07/20 PHP
Thinkphp实现短信验证注册功能
2016/10/18 PHP
php中替换字符串函数strtr()和str_repalce()的用法与区别
2016/11/25 PHP
用js实现的仿sohu博客更换页面风格(简单版)
2007/03/22 Javascript
javascript 获取网页参数系统
2008/07/19 Javascript
childNodes.length与children.length的区别
2009/05/14 Javascript
jQuery技巧大放送 学习jquery的朋友可以看下
2009/10/14 Javascript
自制轻量级仿jQuery.boxy对话框插件代码
2010/10/26 Javascript
JS 退出系统并跳转到登录界面的实现代码
2013/06/29 Javascript
jquery(hide方法)隐藏指定元素实例
2013/11/11 Javascript
jQuery设置和获取HTML、文本和值示例
2014/07/08 Javascript
jQuery+PHP打造滑动开关效果
2014/12/16 Javascript
js实现倒计时及时间对象
2016/11/15 Javascript
JS判断输入的字符串是否是数字的方法(正则表达式)
2016/11/29 Javascript
jQuery实现select模糊查询(反射机制)
2017/01/14 Javascript
JavaScript实现垂直滚动条效果
2017/01/18 Javascript
react.js使用webpack搭配环境的入门教程
2017/08/14 Javascript
vue 做移动端微信公众号采坑经验记录
2018/04/26 Javascript
JavaScript中发出HTTP请求最常用的方法
2018/07/12 Javascript
解决bootstrap-select 动态加载数据不显示的问题
2018/08/10 Javascript
Vue两个版本的区别和使用方法(更深层次了解)
2020/02/16 Javascript
python对字典进行排序实例
2014/09/25 Python
python将文本中的空格替换为换行的方法
2018/03/19 Python
python切片及sys.argv[]用法详解
2018/05/25 Python
Pytorch 计算误判率,计算准确率,计算召回率的例子
2020/01/18 Python
Keras 切换后端方式(Theano和TensorFlow)
2020/06/19 Python
英格兰足协官方商店:England Store
2019/07/12 全球购物
庆中秋节主题活动方案
2014/02/03 职场文书
可口可乐广告词
2014/03/20 职场文书
二年级小学生评语
2014/04/21 职场文书
师德先进个人材料
2014/12/20 职场文书
市场督导岗位职责
2015/04/10 职场文书
少先队入队仪式主持词
2015/07/04 职场文书
Mysql服务添加 iptables防火墙策略的方案
2021/04/29 MySQL
教你怎么用Python处理excel实现自动化办公
2021/04/30 Python