简体   繁体   中英

How to check all items of a list in python are equal to a?

Ok in my script I need it that say base is an array but the ammount of numbers in it can be anything. I want something that can check all items of the list to see if all eliments = 5 heres an example

base = [5, 5, 5, 5]

What code can i use to return a statement that all the numbers are equal to 5.

Use the all -function:

if all(b==5 for b in base):
    do_something()

This is a possible short solution:

k = [5,5,5,5]
answer = 5
k == [answer for i in range(len(k))] # evaluates to True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM