Questions about judging whether lists are equal in Python

Questions about judging whether lists are equal in Python

    This article mainly records the problems encountered in the process of judging whether lists are equal, and summarizes the relevant knowledge about judging whether lists are equal.

0. Origin of problem

    The reason for this article is that in the process of judging whether the lists are equal, there are doubts about the three methods of ==, is, and operator.eq(), so these three methods are summarized as follows.

1. Use the == operation to determine whether the lists are equal

    The code to judge whether the lists are equal using the == operation is as follows:

# 1.使用 == 判断两个列表是否相同
a = [1, 2, 'apple']
b = [1, 2, 'apple']
c = ['apple', 1, 2]
result_a_b = (a == b)
print("result_a_b = {}".format(result_a_b))
print("type(result_a_b) is {}".format(type(result_a_b)))
print("The result of a == b is : {}".format(a == b))
print("The result of a == c is : {}".format(a == c))
print("The result of b == c is : {}".format(b == c))
print('\n')

    The result of the operation is as follows:

result_a_b = True
type(result_a_b) is <class 'bool'>
The result of a == b is : True
The result of a == c is : False
The result of b == c is : False

2. Use the is operation to determine whether the lists are equal

    The code for using the is operation to determine whether the lists are equal is as follows:

# 2.使用 is 操作判断两个列表是否相等
a = [1, 2, 'apple']
b = [1, 2, 'apple']
c = ['apple', 1, 2]
d = a
print("The result of 'a is b' is : {}".format(a is b))
print("The result of 'a is c' is : {}".format(a is c))
print("The result of 'b is c' is : {}".format(b is c))
print("The result of 'a is d' is : {}".format(a is d))   # 因为列表d是列表a的简单赋值操作,所以是相等的
print('\n')

    The result of the operation is as follows:

The result of 'a is b' is : False
The result of 'a is c' is : False
The result of 'b is c' is : False
The result of 'a is d' is : True

    For the concepts of assignment, shallow copy, and deep copy, you can refer to the blog post: Assignment of list list in Python

3. Use operator.eq() to determine whether the lists are equal

    The code to use operator.eq() to determine whether the lists are equal is as follows:


# 3.使用operator.eq()操作判断两个列表是否相等
from operator import *
x = [1, 2, 'apple']
y = [1, 2, 'apple']
z = ['apple', 1, 2]
result_x_y = eq(x, y)
print("The type of result_x_y is : {}".format(type(result_x_y)))
print("The result of eq(x,y) is : {}".format(eq(x, y)))
print("The result of eq(x,z) is : {}".format(eq(x, z)))
print("The result of eq(y,z) is : {}".format(eq(y, z)))
print('\n')

    The result of the operation is as follows:


The type of result_x_y is : <class 'bool'>
The result of eq(x,y) is : True
The result of eq(x,z) is : False
The result of eq(y,z) is : False

4. Summary

    When judging whether the lists are equal, the method of directly using the operator.eq() function is the most direct.

Guess you like

Origin blog.csdn.net/weixin_43981621/article/details/124200061