我创建了2个列表,如下所示
node_dict = {node1 : 1,node2 : 2,node3 : 3}
input_nodes = [n for n in node_dict.keys()]
nodes = [n for n in input_nodes]
其中node1,node2,node3是任意对象
为什么nodes == input_nodes
会返回True
?这些不同的列表对象不是吗?
答案 0 :(得分:1)
在python中,您有两种比较对象的方法:==
和is
在这种情况下:
nodes == input_nodes #True
nodes is input_nodes #False
nodes is input_nodes
对应id(nodes) == id(input_nodes)
,因此检查它们是否是同一个对象。
==
只是检查它们是否相同。
答案 1 :(得分:1)
我认为你正在寻找is
关键字,它比较对象的身份(也就是说,它会检查它们是否是同一个对象)。 ==
仅检查相等性,对于两个列表,这意味着它们在每个索引处具有相同的长度和相同的值。
In [1]: a = range(10)
In [2]: b = range(10)
In [3]: a == b
Out[3]: True
In [4]: a is b
Out[4]: False