如何比较python中的两个列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3726338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two lists in python?
提问by Umesh K
How to compare two lists in python?
如何比较python中的两个列表?
date = "Thu Sep 16 13:14:15 CDT 2010"
sdate = "Thu Sep 16 14:14:15 CDT 2010"
dateArr = [] dateArr = date.split()
sdateArr = [] sdateArr = sdate.split()
Now I want to compare these two lists. I guess split returns a list. We can do simple comparision in Java like dateArr[i] == sdateArr[i], but how can we do it in Python?
现在我想比较这两个列表。我猜 split 返回一个列表。我们可以在 Java 中做简单的比较,比如dateArr[i] == sdateArr[i],但是我们如何在 Python 中做呢?
采纳答案by skajfes
You could always do just:
你总是可以这样做:
a=[1,2,3]
b=['a','b']
c=[1,2,3,4]
d=[1,2,3]
a==b #returns False
a==c #returns False
a==d #returns True
回答by Dominic Rodger
If you mean lists, try ==:
如果您的意思是列表,请尝试==:
l1 = [1,2,3]
l2 = [1,2,3,4]
l1 == l2 # False
If you mean array:
如果你的意思是array:
l1 = array('l', [1, 2, 3])
l2 = array('d', [1.0, 2.0, 3.0])
l1 == l2 # True
l2 = array('d', [1.0, 2.0, 3.0, 4.0])
l1 == l2 # False
If you want to compare strings (per your comment):
如果你想比较字符串(根据你的评论):
date_string = u'Thu Sep 16 13:14:15 CDT 2010'
date_string2 = u'Thu Sep 16 14:14:15 CDT 2010'
date_string == date_string2 # False
回答by Bj?rn Pollex
Given the code you provided in comments, I assume you want to do this:
鉴于您在评论中提供的代码,我假设您想这样做:
>>> dateList = "Thu Sep 16 13:14:15 CDT 2010".split()
>>> sdateList = "Thu Sep 16 14:14:15 CDT 2010".split()
>>> dateList == sdataList
false
The split-method of the string returns a list. A list in Python is very different from an array. ==in this case does an element-wise comparison of the two lists and returns if all their elements are equal and the number and order of the elements is the same. Read the documentation.
split字符串的-method 返回一个列表。Python 中的列表与数组非常不同。==在这种情况下,对两个列表进行逐元素比较,如果它们的所有元素都相等并且元素的数量和顺序相同,则返回。阅读文档。
回答by Escualo
From your post I gather that you want to compare dates, not arrays. If this is the case, then use the appropriate object: a datetimeobject.
从您的帖子中,我了解到您想比较日期,而不是数组。如果是这种情况,则使用适当的对象:一个datetime对象。
Please check the documentation for the datetime module. Dates are a tough cookie. Use reliable algorithms.
请查看datetime 模块的文档。日期是一个艰难的饼干。使用可靠的算法。
回答by PyRsquared
a = ['a1','b2','c3']
b = ['a1','b2','c3']
c = ['b2','a1','c3']
# if you care about order
a == b # True
a == c # False
# if you don't care about order
set(a) == set(b) # True
set(a) == set(c) # True
By casting a, band cas a set, you remove duplicates and order doesn't count. Comparing sets is also much faster and more efficient than comparing lists.
通过强制转换a,b并且c作为一个集合,您可以删除重复项并且订单不计算在内。比较集合也比比较列表更快、更有效。
回答by ravi tanwar
for i in arr1:
if i in arr2:
return 1
return 0
arr1=[1,2,5]
arr2=[2,4,15]
q=checkarrayequalornot(arr1,arr2)
print(q)
>>0

