8

I am trying to combine the lists below to display a date in the format 'dd/hh:mm'.

the lists are as follows:

dd = [23, 23, 24, 24, 24, 24, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27]
hh = [21, 23, 7, 9, 16, 19, 2, 5, 12, 15, 22, 1, 8, 11, 18, 21, 2, 8, 12, 12, 13, 13, 18, 22]
mm = [18, 39, 3, 42, 52, 43, 46, 41, 42, 35, 41, 27, 37, 30, 0, 58, 57, 51, 11, 20, 18, 30, 35, 5]

So combining the lists would look something like

23/21:18, 23/23:39, 24/7:3, 24/9:42 ......

and so on. I tried using a for loop (below) for this, but each time was unsurprisingly met with

finaltimes = []
zip_object = zip(dd,hh,mm)
for list1, list2, list3 in zip_object:
    finaltimes.append(list1+'/'+list2+':'+list3)

TypeError: unsupported operand type(s) for +: 'int' and 'str'

I know I can't combine int and str in this loop but am not sure how to approach this? Any help is appreciated

Georgy
  • 12,464
  • 7
  • 65
  • 73
Alec
  • 87
  • 4
  • 2
    should be `finaltimes.append(str(list1)+'/'+str(list2)+':'+str(list3))` – Pygirl Jul 01 '20 at 19:04
  • 2
    Try `finaltimes = ['{}/{}:{}'.format(*tpl) for tpl in zip(dd, hh, mm)]` – Abdou Jul 01 '20 at 19:04
  • Does this answer your question? [How can I concatenate str and int objects?](https://stackoverflow.com/questions/25675943/how-can-i-concatenate-str-and-int-objects) – Georgy Jul 03 '20 at 12:36

6 Answers6

11

The following should work:

finaltimes = ['{}/{}:{}'.format(*tpl) for tpl in zip(dd, hh, m)]
Abdou
  • 12,931
  • 4
  • 39
  • 42
  • 2
    f-strings are also well suited here `finaltimes = [f'{d}/{h}:{m}' for (d, h, m) in zip(dd, hh, mm)]` (does not change anything, but the tuple looks less readable to me) – arnaud Jul 01 '20 at 19:07
  • I am not particularly fond of f-strings because I believe (*without* much inspection) that they require global lookups, which could be costly. But I hope I am wrong. – Abdou Jul 01 '20 at 19:09
  • Interesting, didn't think about that! – arnaud Jul 01 '20 at 19:11
4

Try some thing like this:

finaltimes.append(f"{list1}/{list2}:{list3}")
Pygirl
  • 12,969
  • 5
  • 30
  • 43
  • This will help OP to understand where is his mistake and @Abdou's response will give him a better solution. – arnaud Jul 01 '20 at 19:11
4

You can use a formatted string:

dd = [23, 23, 24, 24, 24, 24, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27]
hh = [21, 23, 7, 9, 16, 19, 2, 5, 12, 15, 22, 1, 8, 11, 18, 21, 2, 8, 12, 12, 13, 13, 18, 22]
mm = [18, 39, 3, 42, 52, 43, 46, 41, 42, 35, 41, 27, 37, 30, 0, 58, 57, 51, 11, 20, 18, 30, 35, 5]

finaltimes = [f"{d}/{h}:{m}" for d,h,m in zip(dd,hh,mm)]

print(finaltimes)

Output:

['23/21:18',
 '23/23:39',
 '24/7:3',
 '24/9:42',
 '24/16:52',
 '24/19:43',
 '25/2:46',
 '25/5:41',
 '25/12:42',
 '25/15:35',
 '25/22:41',
 '26/1:27',
 '26/8:37',
 '26/11:30',
 '26/18:0',
 '26/21:58',
 '27/2:57',
 '27/8:51',
 '27/12:11',
 '27/12:20',
 '27/13:18',
 '27/13:30',
 '27/18:35',
 '27/22:5']
Red
  • 26,798
  • 7
  • 36
  • 58
1

This gonna work for you:

dd = [23, 23, 24, 24, 24, 24, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27]
hh = [21, 23, 7, 9, 16, 19, 2, 5, 12, 15, 22, 1, 8, 11, 18, 21, 2, 8, 12, 12, 13, 13, 18, 22]
mm = [18, 39, 3, 42, 52, 43, 46, 41, 42, 35, 41, 27, 37, 30, 0, 58, 57, 51, 11, 20, 18, 30, 35, 5]

for day, hor, min in zip(dd, hh, mm):
    print(day, hor, min)
1

We can't combine int and string, so just convert int into string.

So try this:

finaltimes = []
    
zip_object = zip(dd,hh,mm)
    
for list1, list2, list3 in zip_object:
    
    finaltimes.append(str(list1)+'/'+str(list2)+':'+str(list3))

This will concatenate your integers and strings successfully.

ajay sagar
  • 169
  • 1
  • 6
1

I'd do it like this:

dd = [23, 23, 24, 24, 24, 24, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27]
hh = [21, 23, 7, 9, 16, 19, 2, 5, 12, 15, 22, 1, 8, 11, 18, 21, 2, 8, 12, 12, 13, 13, 18, 22]
mm = [18, 39, 3, 42, 52, 43, 46, 41, 42, 35, 41, 27, 37, 30, 0, 58, 57, 51, 11, 20, 18, 30, 35, 5]

output = []
for i in range(0, len(dd)):
    outStr = ""
    outStr = outStr + str(dd[i]) + "/"
    outStr = outStr + str(hh[i]) + ":"
    outStr = outStr + str(mm[i])
    output.append(outStr)

print(output)

Output is:

['23/21:18', '23/23:39', '24/7:3', '24/9:42', '24/16:52', '24/19:43', '25/2:46', '25/5:41', '25/12:42', '25/15:35', '25/22:41', '26/1:27', '26/8:37', '26/11:30', '26/18:0', '26/21:58', '27/2:57', '27/8:51', '27/12:11', '27/12:20', '27/13:18', '27/13:30', '27/18:35', '27/22:5']
CodinGuy
  • 33
  • 1
  • 5