-1

I have a list of times and I want to make a list of lists where the elements of the larger list is a list where the first element is the time and the second element is the number of minutes from a set time. So for example, this is what I have

['2022-04-08, 1:05 pm',
 '2022-04-09, 4:05 pm',
 '2022-04-10, 7:08 pm',
 '2022-04-11, 7:05 pm',
 '2022-04-12, 7:05 pm',
 '2022-04-13, 7:05 pm',
 '2022-04-14, 7:05 pm',
 '2022-04-22, 7:05 pm',
 '2022-04-23, 1:05 pm',
 '2022-04-24, 1:35 pm',
 '2022-04-26, 7:05 pm',
 '2022-04-27, 7:05 pm']

I'd like to have a list like this:

[[['2022-04-08'], ['1:05 pm'],[(minutes after 11:00 A.M)]]
 [['2022-04-09'], ['4:05 pm'],[(minutes after 11:00 A.M)]],
[['2022-04-10'], ['7:08 pm'],[(minutes after 11:00 A.M)]],
 [['2022-04-11'], ['7:05 pm'],[(minutes after 11:00 A.M)]],
 [['2022-04-12'], ['7:05 pm'],[(minutes after 11:00 A.M)]],etc.

3 Answers3

0

You could do something like this:

times = ['2022-04-08, 1:05 pm', 
'2022-04-09, 4:05 pm', 
'2022-04-10, 7:08 pm',
'2022-04-11, 7:05 pm',
'2022-04-12, 7:05 pm',
'2022-04-13, 7:05 pm',
'2022-04-14, 7:05 pm',
'2022-04-22, 7:05 pm',
'2022-04-23, 1:05 pm',
'2022-04-24, 1:35 pm',
'2022-04-26, 7:05 pm',
'2022-04-27, 7:05 pm']
 
import datetime

def convert_time_string(time_to_convert):
    return datetime.datetime.strptime(time_to_convert, '%I:%M %p')

AM_TIME = convert_time_string('11:00 am')

final_list = []
for item in times:
    time_string = item.split(', ')
    # https://stackoverflow.com/questions/14295673/convert-string-into-datetime-time-object
    # https://stackoverflow.com/questions/1759455/how-can-i-account-for-period-am-pm-using-strftime
    time = convert_time_string(time_string[1])
    # https://stackoverflow.com/questions/11743019/convert-python-datetime-to-epoch-with-strftime
    time_after_11 = time - AM_TIME
    min_after_11 = int(time_after_11.total_seconds() / 60)
    
    final_list.append([[time_string[0]], [time_string[1]], [min_after_11]])

print(final_list)
Andrew Ryan
  • 1,489
  • 3
  • 15
  • 21
0

The big ideas here are using a loop so that you only have to think about what happens to each list item, then using the built in datetime package to format your times and calculate time differences:

from datetime import datetime

def transformTimes(times, setTimeString):
    newTimes = []
    setTime = datetime.strptime(setTimeString, "%Y-%m-%d, %I:%M %p")
    for time in times:
        dt = datetime.strptime(time, "%Y-%m-%d, %I:%M %p")
        year = dt.strftime("%Y-%m-%d")
        time = dt.strftime("%I:%M %p")
        diffMinues = (dt - setTime).total_seconds() / 60
        newTimes.append([year, time, diffMinues])
    return newTimes


setTimeString = "2022-04-08, 11:00 am"
newTimes = transformTimes(times, setTimeString)

for time in newTimes:
    print(time)
Temba
  • 370
  • 2
  • 14
0
import datetime
b = [x.split(', ') for x in a]
b = [[[x[0]], [x[1]], [f"{(int) ((datetime.datetime.strptime(x[1], '%I:%M %p') - datetime.datetime.strptime('11:00 am', '%I:%M %p')).total_seconds() // 60)} minutes after 11:00 am"]] for x in b]

where a is your list

bui
  • 1,576
  • 1
  • 7
  • 10