By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,942 Members | 1,788 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,942 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Nov 23 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Nov 23 '06 #2

P: n/a
On Thu, 23 Nov 2006 03:13:10 -0800, Daniel Austria wrote:
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(',')]
--
Steven.

Nov 23 '06 #3

P: n/a

Daniel Austria wrote:
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

Nov 23 '06 #4

P: n/a
On 23/11/06, Steven D'Aprano <st***@remove.this.cybersource.com.auwrote:
On Thu, 23 Nov 2006 03:13:10 -0800, Daniel Austria wrote:
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()
>>int(' 10 ')
10
>>>
:)
Nov 23 '06 #5

P: n/a
Tim Williams wrote:
>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()
>>>int(' 10 ')
10
>>>>
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>

Nov 23 '06 #6

P: n/a
On 23/11/06, Fredrik Lundh <fr*****@pythonware.comwrote:
Tim Williams wrote:
>>>

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 :)
Nov 23 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.