Home > Software design >  Create a list of time in M:S, each value must be repeated 3 times
Create a list of time in M:S, each value must be repeated 3 times

Time:09-21

I need to create a list of time in M:S, but each value must be repeated 3 times.[0:00,0:00,0:00,0:01,0:01,0:01,0:02...] Thanks in advance!

CodePudding user response:

I'll use strings for the times in the list because I don't know which data type you hope to use for them. I'll also use a 24 hour clock. Hope this helps:

[f"{int(m/3)}:{str(int(s/3))}" if s/3 >= 10 else f"{int(m/3)}:{f'0{int(s/3)}'}" for m in range(24*3) for s in range(60*3)]

Here is a longer but easier to understand and modify version.

time_list = []
for m in range(24*3):
  for s in range(60*3):
    secs = str(int(s/3)) if s/3 >= 10 else f'0{int(s/3)}'
    time_list.append(f'{int(m/3))}:{secs}')

This will only work if you are using python 3.6 or above, because that's when f-strings were added. If you're not using a version below 3.6, you can use .format() instead, replacing every instance of f'{x}...{y}' in the code with '{x}...{y}'.format(x, y).

  • Related