Python清单:检查所有项目是否相同 [英] Python list: check if all items are the same or not
本文介绍了Python清单:检查所有项目是否相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个python字符串列表,想检查所有列表项值是否相同.
I have a python list of strings and want to check if all list item values are the same or not.
我尝试使用条件if/then
,但随后我需要检查列表值的所有组合,并且如果列表中有很多项,则需要很多硬代码.
I tried to use condition if/then
but then I'd need to check all combinations of list values and if list have many items then need to many hard code.
if item1 != item1 and item1 != item2 and item1 !=item2 ....... :
check='wrong'
else:
check= 'correct'
输入:
listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
更新
示例:
CORRECT_LIST = ['ep:1000' , 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
在正确的列表中,所有项目的值都相同,那么我的列表是正确的
IN correct list all items values are the same, then my list is correct
WRONG_LIST = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
错误列表中的WRONG_LIST并非所有项目的值字符串都是一些
WRONG_LIST in the wrong list is not all items values string are the some
推荐答案
这会将列表中的每个元素与第一个元素进行比较:
This compares every element of the list to the first one:
listOfStrings = ['ep:1000' , 'ep:4444', 'ep:1000', 'ep:1000', 'ep:1000', 'ep:1000']
check = all(x == listOfStrings[0] for x in listOfStrings)
并为测试用例返回false
.
这篇关于Python清单:检查所有项目是否相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文