0

I have that list:

[(1210, 1229), (1935, 2000), (1536, 1608), (1043, 1120), (1817, 1922), (900, 1023), (1632, 1759)]

Every position in the array represent the time of the activity: (900, 1023) should be (9:00, 10:23)

How can I put the ':' between the hour and the minutes with Python?

1
  • Ideally any time value should be converted to a Python datetime.time object as soon as possible. If you can't do that before the list is constructed, after is okay too. The reason is that while it looks simple now, future changes might need time specific handling that has tricky surprises. Once you have e.g. t = datetime.time(n //100, n % 100), you can use t.strftime("%H:%M") to get a properly formatted time string. Commented Nov 6, 2020 at 19:55

5 Answers 5

2

Get the last two digits, then the first two, and join them:

def num_to_time(num):
    return f"{num // 100}:{num % 100:02}"

l = [(1210, 1229), (1935, 2000), (1536, 1608), (1043, 1120), (1817, 1922), (900, 1023), (1632, 1759)]

[(num_to_time(a), num_to_time(b)) for (a, b) in l]
# [('12:10', '12:29'), ('19:35', '20:00'), ('15:36', '16:08'), ('10:43', '11:20'), ('18:17', '19:22'), ('9:00', '10:23'), ('16:32', '17:59')]
Sign up to request clarification or add additional context in comments.

3 Comments

Doesn't produce the right output e.g. '20:0', '16:8'
return f"{num // 100}:{str(num)[-2:]}"?
@Chris_Rands Fixed.
0

If it is a list of tuples of integers you could run this:

l = [(1210, 1229), (1935, 2000), (1536, 1608), (1043, 1120), (1817, 1922), (900, 1023), (1632, 1759)]
result = []

for x in l:
    time_one = f'{x[0]:0>4}'
    time_two = f'{x[1]:0>4}'

    split_time_one = time_one[:2] + ':' + time_one[2:]
    split_time_two = time_two[:2] + ':' + time_two[2:]
    result.append((split_time_one, split_time_two,))

print(result)
# [('12:10', '12:29'), ('19:35', '20:00'), ('15:36', '16:08'), ('10:43', '11:20'), ('18:17', '19:22'), ('09:00', '10:23'), ('16:32', '17:59')]

Note Python 3.6 or higher is required for this to function.

Comments

0

Here is my shot.

def f(number):
    string = str(number)
    minute = string[-2:]
    hour   = string[:len(minute) - 4]
    return f'{hour}:{minute}'

suite = [(1210, 1229), (1935, 2000), (1536, 1608), (1043, 1120), (1817, 1922), (900, 1023), (1632, 1759)]
for test in suite:
    for string in test:
        print(f(string))

Gives:

12:10
12:29
19:35
20:00
15:36
16:08
10:43
11:20
18:17
19:22
9:00
10:23
16:32
17:59

Check it at colab.google.com

Comments

0

You can use '{:02d}' format :

l = [(1210, 1229), (1935, 2000), (1536, 1608), (1043, 1120), (1817, 1922), (900, 1023), (1632, 1759)]

dates = [('{:02d}:{:02d}'.format(t[0]//100,t[0]%100),'{:02d}:{:02d}'.format(t[1]//100,t[1]%100)) for t in l]

>>> dates
[('12:10', '12:29'), ('19:35', '20:00'), ('15:36', '16:08'), ('10:43', '11:20'), ('18:17', '19:22'), ('09:00', '10:23'), ('16:32', '17:59')]

Comments

0

One more variant to the collection:

array = [(1210, 1229), (1935, 2000), (1536, 1608), (1043, 1120), (1817, 1922), (900, 1023), (1632, 1759)]

def to_time(num): return str(num)[:-2] + ':' + str(num)[-2:]

new_array = [(to_time(a), to_time(b)) for (a,b) in array]

print(new_array)

Output:

[('12:10', '12:29'), ('19:35', '20:00'), ('15:36', '16:08'), ('10:43', '11:20'), ('18:17', '19:22'), ('9:00', '10:23'), ('16:32', '17:59')]

Comments