"10, 20, 30" to [10, 20, 30]

D

Daniel Austria

Sorry,

how can i convert a string like "10, 20, 30" to a list [10, 20, 30]

what i can do is:

s = "10, 20, 30"
tmp = '[' + s + ']'
l = eval(tmp)

but in my opinion this is not a nice solution


daniel
 
T

Tech

Daniel Austria a écrit :
Sorry,

how can i convert a string like "10, 20, 30" to a list [10, 20, 30]

what i can do is:

s = "10, 20, 30"
tmp = '[' + s + ']'
l = eval(tmp)

but in my opinion this is not a nice solution


daniel

If you're sure that there's only ints
l = [int(item) for item in s.split(', ')]

Yannick
 
J

John Machin

Daniel said:
Sorry,

how can i convert a string like "10, 20, 30" to a list [10, 20, 30]

what i can do is:

s = "10, 20, 30"
tmp = '[' + s + ']'
l = eval(tmp)

but in my opinion this is not a nice solution

Most people share your opinion. Try this:

| >>> strg = "10, 20, 30"
| >>> [int(x) for x in strg.split(',')]
| [10, 20, 30]

Cheers,
John
 
S

Steven D'Aprano

Sorry,

how can i convert a string like "10, 20, 30" to a list [10, 20, 30]

what i can do is:

s = "10, 20, 30"
tmp = '[' + s + ']'
l = eval(tmp)

but in my opinion this is not a nice solution


It is a dangerous solution if your data is coming from an untrusted source.
s = "10, 20, 30"
L = [x.strip() for x in s.split(',')]
L ['10', '20', '30']
L = [int(x) for x in L]
L
[10, 20, 30]

Or, as a one liner: [int(x.strip()) for x in s.split(',')]
 
T

Tim Williams

Sorry,

how can i convert a string like "10, 20, 30" to a list [10, 20, 30]

what i can do is:

s = "10, 20, 30"
tmp = '[' + s + ']'
l = eval(tmp)

but in my opinion this is not a nice solution


It is a dangerous solution if your data is coming from an untrusted source.
s = "10, 20, 30"
L = [x.strip() for x in s.split(',')]
L ['10', '20', '30']
L = [int(x) for x in L]
L
[10, 20, 30]

Or, as a one liner: [int(x.strip()) for x in s.split(',')]

You don't need the strip()

:)
 
F

Fredrik Lundh

Tim said:
It is a dangerous solution if your data is coming from an untrusted source.
s = "10, 20, 30"
L = [x.strip() for x in s.split(',')]
L ['10', '20', '30']
L = [int(x) for x in L]
L
[10, 20, 30]

Or, as a one liner: [int(x.strip()) for x in s.split(',')]

You don't need the strip()

and the use of a list comprehension is pretty silly to, given that you want
to apply the same *function* to all items, and don't really need to look
it up for every item:

map(int, s.split(','))

</F>
 
T

Tim Williams

and the use of a list comprehension is pretty silly to, given that you want
to apply the same *function* to all items, and don't really need to look
it up for every item:

map(int, s.split(','))

Haha, thanks Frederic, I wondered how long it would take for a reply
from you :)

"Silly" though ??

Tim :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top