### 比较自己的参数列表？

E.g：

``````a = ['foo', 'foo', 'boo'] #not valid
b = ['foo', 'foo', 'foo'] #valid
``````

#### 7 个答案:

``````if a.count( "foo" ) != len(a)
``````

``````if a.count( a[0] ) != len(a)
``````

...在生产代码中。

``````all(a[0] == x for x in a)
``````

FYI。在输入列表的不同大小上对测试的匹配和不匹配版本进行5000次迭代。

``````List Size 10
0.00530 aList.count(aList[0] ) == len(aList)
0.00699 for with return False if no match found.
0.00892 aList == [aList[0]] * len(aList)
0.00974 len(set(aList)) == 1
0.02334 all(aList[0] == x for x in aList)
0.02693 reduce(lambda x,y:x==y and x,aList)

List Size 100
0.01547 aList.count(aList[0] ) == len(aList)
0.01623 aList == [aList[0]] * len(aList)
0.03525 for with return False if no match found.
0.05122 len(set(aList)) == 1
0.08079 all(aList[0] == x for x in aList)
0.22797 reduce(lambda x,y:x==y and x,aList)

List Size 1000
0.09198 aList == [aList[0]] * len(aList)
0.11862 aList.count(aList[0] ) == len(aList)
0.31874 for with return False if no match found.
0.36145 len(set(aList)) == 1
0.65861 all(aList[0] == x for x in aList)
2.24386 reduce(lambda x,y:x==y and x,aList)
``````

``````def quickExit( aList ):
"""for with return False if no match found."""
value= aList[0]
for x in aList:
if x != value: return False
return True
``````

``````if len(set(my_list)) != 1:
return False
``````

``````all_items_are_same = len(set(my_list)) == 1
``````

``````# Equality returns True or False
all_items_are_same = (len(set(my_list)) == 1)
``````

``````b == [b[0]] * len(b) #valid
a == [a[0]] * len(a) #not valid
``````

``````>>> a = ['foo', 'foo', 'boo'] #not valid
>>> b = ['foo', 'foo', 'foo'] #valid
>>> reduce(lambda x,y:x==y and x,a)
False
>>> reduce(lambda x,y:x==y and x,b)
'foo'
``````