Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining strings and ints to create a date string results in TypeError

Tags:

python

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

like image 606
Alec Avatar asked Jul 01 '20 19:07

Alec


2 Answers

The following should work:

finaltimes = ['{}/{}:{}'.format(*tpl) for tpl in zip(dd, hh, m)]
like image 154
Abdou Avatar answered Oct 01 '22 01:10

Abdou


Try some thing like this:

finaltimes.append(f"{list1}/{list2}:{list3}")
like image 45
Pygirl Avatar answered Oct 01 '22 03:10

Pygirl