比较两个类似列表的对象的内容的最佳方法是什么? [英] What are the best ways to compare the contents of two list-like objects?

查看:77
本文介绍了比较两个类似列表的对象的内容的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我不得不比较两个类似数组的对象(例如list s,tuple s或collection.deque s)的内容时,不管对象的类型如何,我都会使用

When I have to compare the contents of two array-like objects -- for instance lists, tuples or collection.deques -- without regard for the type of the objects, I use

list(an_arrayish) == list(another_arrayish)

还有其他惯用的/更快/更好的方法来实现这一目标吗?

Is there any more idiomatic/faster/better way to achieve this?

推荐答案

按元素进行比较:

def compare(a,b):
    if len(a) != len(b):
        return False
    return all(i == j for i,j in itertools.izip(a,b))

对于Python 3.x,请改用zip

For Python 3.x, use zip instead

这篇关于比较两个类似列表的对象的内容的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆