Check if all elements in a list are identical

前端 未结 22 2201
死守一世寂寞
死守一世寂寞 2020-11-22 07:45

I need a function which takes in a list and outputs True if all elements in the input list evaluate as equal to each other using the standard equal

22条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 07:52

    Or use diff method of numpy:

    import numpy as np
    def allthesame(l):
        return np.all(np.diff(l)==0)
    

    And to call:

    print(allthesame([1,1,1]))
    

    Output:

    True
    

提交回复
热议问题