[英]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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.