Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5310

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

From noydb <jenn.duerr@gmail.com>
Newsgroups comp.lang.python
Subject How best to convert a string "list" to a python list
Date 2011-05-13 10:15 -0700
Organization http://groups.google.com
Message-ID <2efc3a05-22e5-46db-bd22-c95221bbd7b2@k16g2000yqm.googlegroups.com> (permalink)

Show all headers | View raw


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"

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

How best to convert a string "list" to a python list noydb <jenn.duerr@gmail.com> - 2011-05-13 10:15 -0700
  Re: How best to convert a string "list" to a python list Jerry Hill <malaclypse2@gmail.com> - 2011-05-13 13:30 -0400
  Re: How best to convert a string "list" to a python list MRAB <python@mrabarnett.plus.com> - 2011-05-13 18:36 +0100
  Re: How best to convert a string "list" to a python list Redcat <redcat@catfolks.net> - 2011-05-13 19:26 +0000
  Re: How best to convert a string "list" to a python list Nobody <nobody@nowhere.com> - 2011-05-14 09:41 +0100
    Re: How best to convert a string "list" to a python list Daniel Kluev <dan.kluev@gmail.com> - 2011-05-14 21:16 +1100
    Re: How best to convert a string "list" to a python list Terry Reedy <tjreedy@udel.edu> - 2011-05-14 15:59 -0400

csiph-web