I want my output to be:
"fileselection":[0, 1, 2, 3, 4, 5, 6, 7]
My code:
li=[0, 1, 2, 3, 4, 5, 6, 7]
st='"fileselection":'
print st+",".join(li)
It is currently complaining:
TypeError: sequence item 0: expected string, int found.
Just calling str()
on the list makes the output you want:
>>> li = [0, 1, 2, 3, 4, 5, 6, 7]
>>> st = '"fileselection":'
>>> st + str(li)
'"fileselection":[0, 1, 2, 3, 4, 5, 6, 7]'
The problem you see is because the elements of li
are integers, for joining them to a string you need first to cast them to str
. You can do this by applying str()
function via map()
:
>>> st + ','.join(map(str, li))
'"fileselection":0,1,2,3,4,5,6,7'
I want my output to be:
"fileselection":[0, 1, 2, 3, 4, 5, 6, 7]
My code:
li=[0, 1, 2, 3, 4, 5, 6, 7]
st='"fileselection":'
print st+",".join(li)
It is currently complaining:
TypeError: sequence item 0: expected string, int found.
Just calling str()
on the list makes the output you want:
>>> li = [0, 1, 2, 3, 4, 5, 6, 7]
>>> st = '"fileselection":'
>>> st + str(li)
'"fileselection":[0, 1, 2, 3, 4, 5, 6, 7]'
The problem you see is because the elements of li
are integers, for joining them to a string you need first to cast them to str
. You can do this by applying str()
function via map()
:
>>> st + ','.join(map(str, li))
'"fileselection":0,1,2,3,4,5,6,7'
0 commentaires:
Enregistrer un commentaire