How best to convert a string "list" to a python list

N

noydb

I want some code to take the items in a semi-colon-delimted string
"list" and places each in a python list. I came up with below. In
the name of learning how to do things properly, do you experts have a
better way of doing it?

Thanks for any inputs!

***
x = "red;blue;green;yellow" ## string of semi-colon delimited colors

color_list = []
## sc = semi-colon

while x.find(";") <> -1:
sc_pos = x.find(";")
current_color = x[0:sc_pos] ## color w/o sc
current_color_sc = x[0:sc_pos+1] ## color with sc
color_list.append(current_color) ## append color to list
x = x.replace(current_color_sc, "") ## remove color and sc from
string
print current_color
print color_list
print x + "\n"

color_list.append(x) # append last color left in x (no sc at end of
string)
print color_list

print "done"
 
J

Jerry Hill

I want some code to take the items in a semi-colon-delimted string
"list" and places each in a python list.  I came up with below.  In
the name of learning how to do things properly, do you experts have a
better way of doing it?

Strings have a split method, which splits the string into a list based
['red', 'blue', 'green', 'yellow']

That's how I'd do it.
 
M

MRAB

I want some code to take the items in a semi-colon-delimted string
"list" and places each in a python list. I came up with below. In
the name of learning how to do things properly, do you experts have a
better way of doing it?

Thanks for any inputs!

***
x = "red;blue;green;yellow" ## string of semi-colon delimited colors

color_list = []
## sc = semi-colon

while x.find(";")<> -1:
sc_pos = x.find(";")
current_color = x[0:sc_pos] ## color w/o sc
current_color_sc = x[0:sc_pos+1] ## color with sc
color_list.append(current_color) ## append color to list
x = x.replace(current_color_sc, "") ## remove color and sc from
string
print current_color
print color_list
print x + "\n"

color_list.append(x) # append last color left in x (no sc at end of
string)
print color_list

print "done"
['red', 'blue', 'green', 'yellow']
 
R

Redcat

I want some code to take the items in a semi-colon-delimted string
"list" and places each in a python list. I came up with below. In the
name of learning how to do things properly, do you experts have a better
way of doing it?

How about the below?

dan@dan:~/development$ python
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
x = "cyan;magenta;yellow;black"
xList = x.split(';')
xList ['cyan', 'magenta', 'yellow', 'black']
 
N

Nobody

I want some code to take the items in a semi-colon-delimted string "list"
and places each in a python list. I came up with below. In the name of
learning how to do things properly, do you experts have a better way of
doing it?
x = "red;blue;green;yellow" ## string of semi-colon delimited colors

Provided that a semicolon is *always* a delimiter, just use the .split()
method:

color_list = x.split(";")

For more complex formats, where there are quote and/or escape characters
which allow the delimiter to occur as part of an item, you typically need
to use a regular expression to match everything up to the next delimiter,
and do this in a loop to extract the individual items.
 
D

Daniel Kluev

to use a regular expression to match everything up to the next delimiter,
and do this in a loop to extract the individual items.

re.findall() should let you match all items at once, without loop.
 
T

Terry Reedy

Provided that a semicolon is *always* a delimiter, just use the .split()
method:

color_list = x.split(";")

For more complex formats, where there are quote and/or escape characters
which allow the delimiter to occur as part of an item, you typically need
to use a regular expression to match everything up to the next delimiter,
and do this in a loop to extract the individual items.

Or, for some formats, use the cvs module.
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top