簡體   English   中英

查找元素的值是唯一的,並且列表中的所有元素都相同

[英]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.

始終列出格式:

   a = ["1", "2", "3","4"]
   b = ["1-2-3", "1-2-2"] # That is dash separated 

您可以嘗試使用附加條件拆分-並檢查長度是否與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)

結果:

True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM