检查列表中的所有元素在Python中是否相同
生活随笔
收集整理的這篇文章主要介紹了
检查列表中的所有元素在Python中是否相同
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Here, we are implementing a python program to check whether all elements of a list are the same or not?
在這里,我們正在實現一個python程序來檢查列表中的所有元素是否相同?
We can use [1:] and [:-1] to compare all the elements in the given list.
我們可以使用[1:]和[:-1]比較給定列表中的所有元素。
Program:
程序:
# function to check elements def check_equal(a):return a[1:] == a[:-1]# lists x = [10, 20, 30, 40,50] y = [10, 20, 20, 20, 20] z = [10, 10, 10, 10, 10]# check how [1:] and [:-1] wors? print("x: ", x) print("x[1:]: ", x[1:]) print("x[:-1]: ", x[:-1]) print("check_equal(x): ",check_equal(x)) print()print("y: ", y) print("y[1:]: ", y[1:]) print("y[:-1]: ", y[:-1]) print("check_equal(y): ",check_equal(y)) print()print("z: ", z) print("z[1:]: ", z[1:]) print("z[:-1]: ", z[:-1]) print("check_equal(z): ",check_equal(z)) print()Output
輸出量
x: [10, 20, 30, 40, 50] x[1:]: [20, 30, 40, 50] x[:-1]: [10, 20, 30, 40] check_equal(x): Falsey: [10, 20, 20, 20, 20] y[1:]: [20, 20, 20, 20] y[:-1]: [10, 20, 20, 20] check_equal(y): Falsez: [10, 10, 10, 10, 10] z[1:]: [10, 10, 10, 10] z[:-1]: [10, 10, 10, 10] check_equal(z): True翻譯自: https://www.includehelp.com/python/check-all-elements-of-a-list-are-the-same-or-not.aspx
總結
以上是生活随笔為你收集整理的检查列表中的所有元素在Python中是否相同的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人工智能ai知识_人工智能中基于知识的代
- 下一篇: Java ClassLoader fin