简体   繁体   中英

Subtracting datetimes in two different lists

Junior Dev here.

I'm looking to subtract two lists of date times. I'm encountering a TypeError: list indices must be integers or slices, not datetime.datetime Any suggestions on how to subtract these timestamps and print them?

My first list contains...

start_times = [datetime.datetime(2022, 6, 21, 13, 1, 40, 757460), datetime.datetime(2022, 6, 21, 13, 1, 48, 308809), datetime.datetime(2022, 6, 21, 13, 1, 48, 322679), datetime.datetime(2022, 6, 21, 13, 1, 48, 327886), datetime.datetime(2022, 6, 21, 13, 1, 48, 329119), datetime.datetime(2022, 6, 21, 13, 1, 48, 332553), datetime.datetime(2022, 6, 21, 13, 1, 48, 335256), datetime.datetime(2022, 6, 21, 13, 1, 48, 339678), datetime.datetime(2022, 6, 21, 13, 1, 48, 353079), datetime.datetime(2022, 6, 21, 13, 1, 48, 374466), datetime.datetime(2022, 6, 21, 13, 1, 48, 387423), datetime.datetime(2022, 6, 21, 13, 1, 48, 427936)]

My second list contains...

end_times = [datetime.datetime(2022, 6, 21, 13, 1, 48, 308804), datetime.datetime(2022, 6, 21, 13, 1, 48, 322677), datetime.datetime(2022, 6, 21, 13, 1, 48, 327884), datetime.datetime(2022, 6, 21, 13, 1, 48, 329117), datetime.datetime(2022, 6, 21, 13, 1, 48, 332551), datetime.datetime(2022, 6, 21, 13, 1, 48, 335255), datetime.datetime(2022, 6, 21, 13, 1, 48, 339676), datetime.datetime(2022, 6, 21, 13, 1, 48, 353077), datetime.datetime(2022, 6, 21, 13, 1, 48, 374465), datetime.datetime(2022, 6, 21, 13, 1, 48, 387421), datetime.datetime(2022, 6, 21, 13, 1, 48, 427935), datetime.datetime(2022, 6, 21, 13, 1, 48, 427937)]

I use the following to fill my lists respectfully...

start_times.append(datetime.now())

I have the following function that calculates the deltas in between and prints out the desired deltas. Note, that there is always a 1:1 relationship between start and end time.

def print_script_runtimes(start_times, end_times):
   for time in end_times:
       print(str(end_times[time] - start_times[time]))

You are iterating over the values of end_times , not the indices.

Zip the lists together, then subtract the elements of the resulting tuples.

def print_script_runtimes(start_times, end_times):
    for start, end in zip(start_times, end_times):
        print(end - start)

You can also use map to apply operator.sub immediately.

from operator import sub


def print_script_runtimes(start_times end_times):
    for runtime in map(sub, end_times, start_times):
        print(runtime)

The only mistake in your code is that you are iterating over the values of end_times .

def print_script_runtimes(start_times, end_times):
   for i in range(len(end_times):
       print(str(end_times[i] - start_times[i]))

EDIT: chepner's solutions are certainly better, despite the only error in your code being the one I wrote above.

You need to change your 'for' loop. The way you wrote the code, the variable 'time' returns the datetime object in the list, not it's index. To get the index, you need something like this:

for i in range(len(endtimes)):
   print(str(end_times[i] - start_times[i]))

Here's an example using enumerate() as I suggested in my comment. A range() example has been provided by others.

def print_script_runtimes(start_times, end_times):
   for x,time in enumerate(end_times):
       print(str(end_times[x] - start_times[x]))
       # alternatively:  print(str(time - start_times[x]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM