Eri*_*rik 6 python polymorphism idioms
当我必须比较两个类似数组的对象的内容 - 例如lists,tuples或collection.deques - 而不考虑对象的类型时,我使用
list(an_arrayish) == list(another_arrayish)
Run Code Online (Sandbox Code Playgroud)
有没有更惯用/更快/更好的方法来实现这一目标?
按元素比较:
def compare(a,b):
if len(a) != len(b):
return False
return all(i == j for i,j in itertools.izip(a,b))
Run Code Online (Sandbox Code Playgroud)
对于 Python 3.x,请zip改用