假设我有这样的代码:
a = [1,2]
b = [1,2]
def equivalentButDifferent(list1, list2):
# list1 and list2 are lists
return <is list1 and list2 the same or merely equivalent>
如何为equivalentButDifferent(a,b)
返回True,为equivalentButDifferent(a,a)
返回False?
正如@jonrsharpe在评论中所说,这将是函数:
a = [1,2]
b = [1,2]
def equivalentButDifferent(list1, list2):
return list1 == list2 and list1 is not list2
==或!=运营商比较股权价值。在这种情况下:
>>> a = [1,2]
>>> b = [1,2]
>>> a == b
True
>>> a != b
False
也就是说,a和b是相等的。
is和not is比较标识的值(如果它们相同(。
>>> a = [1,2]
>>> b = [1,2]
>>> c = a
>>> a is b
False
>>> a is c
True