[英]Find elements values are unique and all elements are same in the list
Find elements values are unique and all elements are same in the list. 查找元素的值是唯一的,并且列表中的所有元素都相同。
>>> 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.
list format always: 始终列出格式:
a = ["1", "2", "3","4"]
b = ["1-2-3", "1-2-2"] # That is dash separated
You can try with additional condition to split on -
and check if length matches with set
as compared to list
ie add (len(x.split('-')) == len(set(x.split('-')))
: 您可以尝试使用附加条件拆分
-
并检查长度是否与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)
Result: 结果:
False
For other example: 对于其他示例:
>>> a = ['1-2-3','1-2-3']
>>> all((x == a[0]) and (len(x.split('-')) == len(set(x.split('-')))) for x in a)
Result: 结果:
True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.