开发者

Is there a way to see if two lists are the exact same without looping in python?

开发者 https://www.devze.com 2023-04-07 05:18 出处:网络
Say I have: a = [1, 2, 3] b = [1, 2, 3] is there a way to test the lists to see if they are the same, without having to loop through each entry?

Say I have:

a = [1, 2, 3]
b = [1, 2, 3]

is there a way to test the lists to see if they are the same, without having to loop through each entry?

Here's what I was thinking..I know to check if two variables are the same I could use:

id(a)

but it doesn't work because the I开发者_高级运维D's are different so is there some type of checksum or way that python stores values of the table so I can simply compare two variables?


Doesn't == work?

>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a == b
True


the == operator should function as expected on lists

>>> x = [1, 2]  
>>> y = [1, 2]
>>> x == y
True


the == operator should function works on lists

This output is received on certain versions of python, I don't know which.

>>> import numpy as np
>>> x = [1, 2, 3, 4]  
>>> y = [1, 2, 3, 4]
>>> z = [1, 2, 2, 4]
>>> x == y
[True, True, True, True]
>>> x == z
[True, True, False, True]

After this, just use numpy to determine the entire list.

>>> np.all(x == y)
True
>>> np.all(x == z)
False

or if only a single similarity is required:

>>> np.any(x == z)
True
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号