1

Say you have this list of floats assuming we have an even number of entries :

[1.0, 2.0, 3.0, 4.0, 5.0, 6.0] 

How could one turn this into this string :

[1.0, 2.0, 3.0][4.0, 5.0, 6.0] 

If we had 10 elements we would have :

[1.0, 2.0, 3.0, 4.0, 5.0][6.0, 7.0, 8.0, 9.0, 10.0]

etc.

I tried :

list_to_str = ' '.join([str(e) for e in total_list])
final_str = '[' + list_to_str + ']'

But with this, the first '[' and the last ']' are placed only at the beginning and at the end of the string... the middle ones are missing...

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
user
  • 71
  • 5

3 Answers3

1

Try this:

total_list = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]

str(total_list[:len(total_list)//2]) + str(total_list[len(total_list)//2:])

#'[1.0, 2.0, 3.0][4.0, 5.0, 6.0]'
IoaTzimas
  • 10,538
  • 2
  • 13
  • 30
1

you can try list-comprehension

' '.join(map(str, [total_list[i: i+len(total_list)//2] for i in range(0, len(total_list), len(total_list)//2)]))
'[1.0, 2.0, 3.0] [4.0, 5.0, 6.0]'
Epsi95
  • 8,832
  • 1
  • 16
  • 34
1

I'd try something like this.

yourLst = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] 
middle = len(yourLst)//2
lsts = [yourLst[:middle],yourLst[middle:]]
yourString = ''.join(str(lst) for lst in lsts)

output

[1.0, 2.0, 3.0][4.0, 5.0, 6.0]

and for those who crave one line code,

yourLst = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] 
yourString = ''.join(str(lst) for lst in [yourLst[:len(yourLst)//2],yourLst[len(yourLst)//2:]])
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44