曼查克 发表于 2021-1-15 10:34:35

python列表比较

本帖最后由 曼查克 于 2021-1-21 08:32 编辑

请教一下,有没有比较便捷的判断两个列表是否相等的方法?

百度了好一会都没有找到。。




后经尝试,这样子是可以的:
    if == :
      print('d')

尝试下来感觉不建议这么做,简单的list不会有问题,比较复杂的情况下可能会导致各种问题,可能因为list里的数据形式可以是多样的。
之前的报错就是因为我的list里放的是ndarray。
如有疑问欢迎指教。

jackz007 发表于 2021-1-15 10:57:18

      不可以直接比吗?
>>> a =
>>> b =
>>> a == b
False
>>> c =
>>> a == c
True
>>>

曼查克 发表于 2021-1-15 11:28:31

jackz007 发表于 2021-1-15 10:57
不可以直接比吗?

会报错:
    if contours == contours2:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

jackz007 发表于 2021-1-15 11:54:01

if contours == contours2:
改为
if (contours == contours2) . any() :

曼查克 发表于 2021-1-15 11:59:54

本帖最后由 曼查克 于 2021-1-15 12:00 编辑

jackz007 发表于 2021-1-15 11:54
改为

试过了。
    if (contours == contours2).any():
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


    if (contours == contours2).all():
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Daniel_Zhang 发表于 2021-1-15 16:07:35

if contours.all() == contours2.all():

曼查克 发表于 2021-1-18 11:23:29

Daniel_Zhang 发表于 2021-1-15 16:07


这样也不行欸。
    if contours.all() == contours2.all():
AttributeError: 'list' object has no attribute 'all'

曼查克 发表于 2021-1-18 11:57:52

这样子是可以的:
    if == :
      print('d')

尝试下来感觉不建议这么做,简单的list不会有问题,比较复杂的情况下可能会导致各种问题,可能因为list里的数据形式可以是多样的。
之前的报错就是因为我的list里放的是array。
如有疑问欢迎指教。

qq1151985918 发表于 2021-1-18 12:20:19

本帖最后由 qq1151985918 于 2021-1-18 12:30 编辑

if all(contours == contours2):如果报错试试import numpy as np
if all(np.array(contours) == np.array(contours2)):

曼查克 发表于 2021-1-20 11:49:19

qq1151985918 发表于 2021-1-18 12:20
如果报错试试

    if all(np.array(contours) == np.array(contours2)):
TypeError: 'bool' object is not iterable

qq1151985918 发表于 2021-1-20 12:16:37

曼查克 发表于 2021-1-20 11:49


if all(contours == contours2):试过了吗?

曼查克 发表于 2021-1-20 13:32:02

qq1151985918 发表于 2021-1-20 12:16
试过了吗?

    if all(contours == contours2):
TypeError: 'bool' object is not iterable

一样的问题。

Python初学者8号 发表于 2021-1-20 18:31:31

曼查克 发表于 2021-1-18 11:57
这样子是可以的:




我看了一圈没看到你的contours contours2 是啥

曼查克 发表于 2021-1-21 08:20:36

本帖最后由 曼查克 于 2021-1-21 08:32 编辑

Python初学者8号 发表于 2021-1-20 18:31
我看了一圈没看到你的contours contours2 是啥

是一组元素都是ndarray的列表,上面有解释过。{:5_109:}

Python初学者8号 发表于 2021-1-21 09:09:25

曼查克 发表于 2021-1-21 08:20
是一组元素都是ndarray的列表,上面有解释过。

我以为,是有具体值的,我有时候觉得有具体值讨论起来清除一些

曼查克 发表于 2021-1-21 09:33:03

Python初学者8号 发表于 2021-1-21 09:09
我以为,是有具体值的,我有时候觉得有具体值讨论起来清除一些

列表里每个元素都是像这样的:
array([[],

       [],

       [],

       [],

       [],

       []], dtype=int32)
整个列表太长就不复制了。

Python初学者8号 发表于 2021-1-22 13:28:46

曼查克 发表于 2021-1-21 09:33
列表里每个元素都是像这样的:

整个列表太长就不复制了。

哦哦哦这样啊{:5_92:}恕我愚笨,感觉直接使用==应该可以吧

Minecraft程序猿 发表于 2021-2-6 19:02:02

一般相同的数据在Python里只存一份可以试试 is 关键字来比较其内存地址,
print(a is b) #返回True或False
你也可以比较每个元素:
for each in a:
    if each == b:
      continue
    else:
      print(False)
      break
页: [1]
查看完整版本: python列表比较