简体   繁体   English

如何检查python中列表的所有项目是否等于a?

[英]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. 好的,在我的脚本中,我需要说base是一个数组,但是其中的数字可以是任何东西。 I want something that can check all items of the list to see if all eliments = 5 heres an example 我想要一些可以检查列表中所有项目的东西,以查看是否所有元素= 5

base = [5, 5, 5, 5]

What code can i use to return a statement that all the numbers are equal to 5. 我可以使用什么代码返回所有数字均等于5的语句。

Use the all -function: 使用all功能:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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