[Tutor] Convert a string of numbers to a list
Kyle Kwaiser
kkwaiser at umich.edu
Fri Feb 27 22:40:31 CET 2009
Turns out I was close but had not combined everything. I never would
have picked up on the map() function. Much more efficient than looping
through the whole mess and converting to int.
x = ['[335, 180, 201, 241, 199]\r\n']
y = map( int, x[0].strip( '[]\r\n' ).split( ', ' ) ) #need an index here
print y
[335, 180, 201, 241, 199]
Thanks for your help!
kbk
Kent Johnson wrote:
> For one line:
> In [11]: s = '[335, 180, 201, 241, 199]\r\n'
>
> In [12]: map(int, s.strip('[]\r\n').split(', '))
> Out[12]: [335, 180, 201, 241, 199]
>
> Modify appropriately to handle a list of lines.
>
> Kent
More information about the Tutor
mailing list