meteor23
meteor23

Reputation: 287

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 

Upvotes: 0

Views: 61

Answers (1)

niraj
niraj

Reputation: 18208

You can try with additional condition to split on - and check if length matches with set as compared to list i.e. add (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

Upvotes: 2

Related Questions