日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python判断数组中是否存在重复元素_利用python查看数组中的所有元素是否相同

發布時間:2025/3/21 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python判断数组中是否存在重复元素_利用python查看数组中的所有元素是否相同 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

不知道大家有沒有過這種經歷,就是想要判斷兩個數組運算后得到的新數組中的各個元素值是否相同。這里給出一種使用np.unique()的方法,代碼如下:cbc免費資源網

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的時候,數組中存在不一樣的元素值。cbc免費資源網

如果數組中的元素是復數呢?cbc免費資源網

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)。cbc免費資源網

總結

以上是生活随笔為你收集整理的python判断数组中是否存在重复元素_利用python查看数组中的所有元素是否相同的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。