如何简单检查数组中的所有元素是否相同?
答
您可以使用all
:
if all(i == a[0] for i in a):
#all the elements are the same
pass
您也可以使用一组:
if len(set(a)) == 1:
#all the elements are the same
pass
'LEN(集(my_array))== 1'? – PRMoureu
将它转换为一个集合,并检查它的大小为1. Bah只是打到它 –
我不同意将这篇文章标记为重复的,因为它与''''numpy.array'''有关,而不是''' 'list'''。而且,是的,你可以用'''numpy.all(your_array == your_array [0])检查身份''' –