tags:

views:

152

answers:

4

How to compare two arrays in 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 array I guess split returns array. We can do simple comparision in Java like dateArr[i] == sdateArr[i] but how can we do it in Python?

Sorry for trouble Please help

+2  A: 

If you mean lists, try ==:

l1 = [1,2,3]
l2 = [1,2,3,4]

l1 == l2 # False

If you mean 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
Dominic Rodger
I want something like
u3050
dateArray = Thu Sep 16 13:14:15 CDT 2010
u3050
@Umesh - how is that an array?
Dominic Rodger
sdateArray = Thu Sep 16 14:14:15 CDT 2010
u3050
Now both these dates were string I created array out of that but how do I compare them using for loop?
u3050
@Umesh - post the code where you created the two strings, otherwise all of us are just guessing at what you're trying to do.
Dominic Rodger
@Umesh: this information belongs in the body of the question, not comments.
SilentGhost
I know it looks like string you can understand them as arrays.
u3050
Sorry I will put code
u3050
I am sorry for the trouble actually I am using StackOverflow for the first time so not familiar with UI.
u3050
date= "Thu Sep 16 13:14:15 CDT 2010"
u3050
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 array I guess split returns array. We can do simple comparision in Javlike dateArr[i] == sdateArr[i] but how can we do it in Java?Sorry for trouble Please help
u3050
@Umesh Kacha: Scroll up and look at your question. Below it there are several buttons. The second from the left says `edit`. This can be used to edit your question, for instance if you want to add additional information.
Space_C0wb0y
+5  A: 

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
skajfes
A: 

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.

Space_C0wb0y
A: 

From your post I gather that you want to compare dates, not arrays. If this is the case, then use the appropriate object: a datetime object.

Please check the documentation for the datetime module. Dates are a tough cookie. Use reliable algorithms.

Arrieta