我需要找出两个列表在引用方面是否相等,在价值方面是否相等
I need to find out if two lists are equal in terms of reference, equal in terms of value or not equal
def type_of_equality(list1, list2):
new_string = ""
if list1 == list2:
new_string += "value"
return new_string
elif list1 != list2:
new_string += "not equal"
return new_string
elif list1 is list2:
new_string += "reference"
return new_string
当我尝试这个时
x = [1,2,3]
y = x
print(type_of_equality(x, y))
输出应该是参考,但输出是相等的。我该如何解决这个问题。
在检查是否相等之前,您应该检查是否 list1 is list2
。
当 x 为 y 时,x 始终等于 y。
def type_of_equality(list1, list2):
new_string = ""
if list1 == list2:
new_string += "value"
return new_string
elif list1 != list2:
new_string += "not equal"
return new_string
elif list1 is list2:
new_string += "reference"
return new_string
当我尝试这个时
x = [1,2,3]
y = x
print(type_of_equality(x, y))
输出应该是参考,但输出是相等的。我该如何解决这个问题。
在检查是否相等之前,您应该检查是否 list1 is list2
。
当 x 为 y 时,x 始终等于 y。