查找元素值是唯一的,并且列表中的所有元素都是相同的。
>>> a = ['1','1']
>>> all(x == a[0] for x in a)
True
>>> a = ['1','2']
>>> all(x == a[0] for x in a)
False
>>> a = ['1-2-3','1-2-3']
>>> all(x == a[0] for x in a)
True
#### Diffent Example #####################
>>> a = ['1-2-2','1-2-2']
>>> all(x == a[0] for x in a)
True
Expected Output False.
any elements must contain unique values, but here it is repeated that is 2-2.列表格式始终:
a = ["1", "2", "3","4"]
b = ["1-2-3", "1-2-2"] # That is dash separated 发布于 2018-05-21 13:08:49
您可以尝试使用附加条件在-上拆分,并检查长度是否与set匹配,而不是list,即添加(len(x.split('-')) == len(set(x.split('-')))
>>> a = ['1-2-2','1-2-2']
>>> all((x == a[0]) and (len(x.split('-')) == len(set(x.split('-')))) for x in a)结果:
False对于其他示例:
>>> a = ['1-2-3','1-2-3']
>>> all((x == a[0]) and (len(x.split('-')) == len(set(x.split('-')))) for x in a)结果:
Truehttps://stackoverflow.com/questions/50442214
复制相似问题