Reputation: 43
I have two list here:
list1 = [u'2018-05-06T15:53:05.000-0400', '2018-05-06T17:53:05.000-0400']
list2 = [u'2018-05-06T15:32:24.000-0400', u'2018-05-06T15:32:29.000-0400', u'2018-05-06T15:32:36.000-0400', u'2018-05-06T15:53:05.000-0400', u'2018-05-06T16:42:41.000-0400', u'2018-05-10T00:16:12.000-0400', u'2018-05-10T00:16:23.000-0400', u'2018-05-12T17:37:42.000-0400']
I'm trying to compare the elements in two lists and return the minimum value of the different for both element 0 in list1 and element 1 in list2, for example:
for value1 in list1:
value1 = datetime.strptime(value1, '%Y-%m-%dT%H:%M:%S.000-0400')
for value2 in list2:
value2 = datetime.strptime(value2, '%Y-%m-%dT%H:%M:%S.000-0400')
if value1 < value2:
value3 = value2 - value1
list3.append(value3)
print list3
print list3 give me:
[datetime.timedelta(0, 2976), datetime.timedelta(3, 30187), datetime.timedelta(3, 30198), datetime.timedelta(6, 6277), datetime.timedelta(3, 22987), datetime.timedelta(3, 22998), datetime.timedelta(5, 85477)]
Everything in the same list.
Here in list3[0]
, I could get the minimum value for first element in list1
but how could I get the minimum value for the second one list3[3]
and loop if i have element 3 in list1?
I would like to separate the list for compare result for first element in list1
and second element, please let me know how to achieve this, much appreciated!
Upvotes: 2
Views: 1124
Reputation: 59274
If I understand correctly, you can separate your time deltas in different lists, and then just use min
to get the minimum values.
Using your own code:
list3=[]
for value1 in list1:
value1 = datetime.strptime(value1, '%Y-%m-%dT%H:%M:%S.000-0400')
aux_list = []
for value2 in list2:
value2 = datetime.strptime(value2, '%Y-%m-%dT%H:%M:%S.000-0400')
if value1 < value2:
value3 = value2 - value1
aux_list.append(value3)
list3.append(aux_list)
This way, your list3
holds the difference from each element of list1
to all elements of list2
>>> print(list3)
[[datetime.timedelta(0, 2976), datetime.timedelta(3, 30187), datetime.timedelta(3, 30198), datetime.timedelta(6, 6277)],
[datetime.timedelta(3, 22987), datetime.timedelta(3, 22998), datetime.timedelta(5, 85477)]]
Then you can get the minimum difference from the first item using min(list[0])
and for the second min(list[1])
Notice that, if you have
l1 = [datetime.strptime(x, '%Y-%m-%dT%H:%M:%S.000-0400') for x in list1]
l2 = [datetime.strptime(x, '%Y-%m-%dT%H:%M:%S.000-0400') for x in list2]
your whole code can be written in one line using list comprehension:
k = [min([v2 - v1 for v2 in l2 if v2>v1]) for v1 in l1]
being l1
and l2
your list of datetime
objects. This yields
0:49:36 # k[0]
3 days, 6:23:07 # k[1]
Upvotes: 2
Reputation: 334
If you want to iterate over the two lists together you can zip them and iterate over them simultaneously. Then compare the values and append the one you want to the result list.
list1 = [1, 2, 3]
list2 = [0, 3]
list3 = []
for (x, y) in zip(list1, list2):
z = min(x, y)
list3.append(z)
Note that the result will be the size of the smaller list, list2 in this case.
Upvotes: 0
Reputation: 2299
First off, Python doesn't know that the string represents a date. You can't just throw data into a string and expect the interpreter to know what you want. So, you're just going to get an error because the "-" operator means nothing to strings.
If you want to compare two dates, I suggest reading up on Python's builtin datetime module: https://docs.python.org/2/library/datetime.html
Upvotes: 0