Check if all elements in a list are identical

前端 未结 22 2351
死守一世寂寞
死守一世寂寞 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:59

    If you're interested in something a little more readable (but of course not as efficient,) you could try:

    def compare_lists(list1, list2):
        if len(list1) != len(list2): # Weed out unequal length lists.
            return False
        for item in list1:
            if item not in list2:
                return False
        return True
    
    a_list_1 = ['apple', 'orange', 'grape', 'pear']
    a_list_2 = ['pear', 'orange', 'grape', 'apple']
    
    b_list_1 = ['apple', 'orange', 'grape', 'pear']
    b_list_2 = ['apple', 'orange', 'banana', 'pear']
    
    c_list_1 = ['apple', 'orange', 'grape']
    c_list_2 = ['grape', 'orange']
    
    print compare_lists(a_list_1, a_list_2) # Returns True
    print compare_lists(b_list_1, b_list_2) # Returns False
    print compare_lists(c_list_1, c_list_2) # Returns False
    

提交回复
热议问题