Python: determining whether any item in sequence is equal to any other

前端 未结 5 1067
失恋的感觉
失恋的感觉 2021-01-04 19:40

I\'d like to compare multiple objects and return True only if all objects are not equal among themselves. I tried using the code below, but it doesn\'t work. If

5条回答
  •  走了就别回头了
    2021-01-04 20:12

    @Michael Hoffman's answer is good if the objects are all hashable. If not, you can use itertools.combinations:

    >>> all(a != b for a, b in itertools.combinations(['a', 'b', 'c', 'd', 'a'], 2))
    False
    >>> all(a != b for a, b in itertools.combinations(['a', 'b', 'c', 'd'], 2))
    True
    

提交回复
热议问题