如何比较2D列出了在Python平等? [英] How do I compare 2D lists for equality in Python?
问题描述
由于两个列表:
A = [1,2],[3,4]
B = [1,2],[3,4]
我怎么会写比较
这样:
比较(A,B)=>真正
你想这样的:
>>>一个= [[1,2],[3,4]
>>> B = [1,2],[3,4]
>>>一个== b
真正
请注意: ==
不是有用列表是无序的,如(在的通知顺序
,和在 b
的)
>>>一个= [[3,4],[1,2]]
>>> B = [1,2],[3,4]
>>>一个== b
假
有关进一步的参考这个问题:如何在python比较列表/集列表?
修改:感谢@dr jimbob
如果您想排序,您可以使用后比较排序(一)==排序(B)
。结果
但同样一个观点,如果 C = [4,3],[2,1]]
然后排序(C)==排序(一)==假
,因为排序(C)
是不同的 [2,1],[4,3] ]
(不深入排序的)
<子>这个你必须使用来自链接答题技巧。由于我正在学习Python的过:)
Given two lists:
a = [[1,2],[3,4]]
b = [[1,2],[3,4]]
How would I write compare
such that:
compare(a,b) => true
Do you want this:
>>> a = [[1,2],[3,4]]
>>> b = [[1,2],[3,4]]
>>> a == b
True
Note: ==
not useful when List are unordered e.g (notice order in a
, and in b
)
>>> a = [[3,4],[1,2]]
>>> b = [[1,2],[3,4]]
>>> a == b
False
See this question for further reference: How to compare a list of lists/sets in python?
Edit: Thanks to @dr jimbob
If you want to compare after sorting you can use sorted(a)==sorted(b)
.
But again a point, if c = [[4,3], [2,1]]
then sorted(c) == sorted(a) == False
because, sorted(c)
being different [[2,1],[4,3]]
(not in-depth sort)
for this you have to use techniques from linked answer. Since I am learning Python too :)
这篇关于如何比较2D列出了在Python平等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!