Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #5310 > unrolled thread
| Started by | noydb <jenn.duerr@gmail.com> |
|---|---|
| First post | 2011-05-13 10:15 -0700 |
| Last post | 2011-05-14 15:59 -0400 |
| Articles | 7 — 7 participants |
Back to article view | Back to comp.lang.python
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
| From | noydb <jenn.duerr@gmail.com> |
|---|---|
| Date | 2011-05-13 10:15 -0700 |
| Subject | How best to convert a string "list" to a python list |
| Message-ID | <2efc3a05-22e5-46db-bd22-c95221bbd7b2@k16g2000yqm.googlegroups.com> |
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"
[toc] | [next] | [standalone]
| From | Jerry Hill <malaclypse2@gmail.com> |
|---|---|
| Date | 2011-05-13 13:30 -0400 |
| Message-ID | <mailman.1519.1305307841.9059.python-list@python.org> |
| In reply to | #5310 |
On Fri, May 13, 2011 at 1:15 PM, noydb <jenn.duerr@gmail.com> wrote:
> 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
on a delimiter, like this:
>>> x = "red;blue;green;yellow"
>>> color_list = x.split(";")
>>> print color_list
['red', 'blue', 'green', 'yellow']
That's how I'd do it.
--
Jerry
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2011-05-13 18:36 +0100 |
| Message-ID | <mailman.1521.1305308221.9059.python-list@python.org> |
| In reply to | #5310 |
On 13/05/2011 18:15, noydb wrote:
> 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"
>>> x = "red;blue;green;yellow"
>>> x.split(";")
['red', 'blue', 'green', 'yellow']
[toc] | [prev] | [next] | [standalone]
| From | Redcat <redcat@catfolks.net> |
|---|---|
| Date | 2011-05-13 19:26 +0000 |
| Message-ID | <935evgF8cmU1@mid.individual.net> |
| In reply to | #5310 |
On Fri, 13 May 2011 10:15:29 -0700, noydb wrote:
> 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']
>>>
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2011-05-14 09:41 +0100 |
| Message-ID | <pan.2011.05.14.08.40.47.47000@nowhere.com> |
| In reply to | #5310 |
On Fri, 13 May 2011 10:15:29 -0700, noydb wrote:
> 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.
[toc] | [prev] | [next] | [standalone]
| From | Daniel Kluev <dan.kluev@gmail.com> |
|---|---|
| Date | 2011-05-14 21:16 +1100 |
| Message-ID | <mailman.1545.1305368166.9059.python-list@python.org> |
| In reply to | #5351 |
On Sat, May 14, 2011 at 7:41 PM, Nobody <nobody@nowhere.com> wrote: > 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. -- With best regards, Daniel Kluev
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2011-05-14 15:59 -0400 |
| Message-ID | <mailman.1560.1305403210.9059.python-list@python.org> |
| In reply to | #5351 |
On 5/14/2011 4:41 AM, Nobody wrote:
> On Fri, 13 May 2011 10:15:29 -0700, noydb wrote:
>
>> 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.
Or, for some formats, use the cvs module.
--
Terry Jan Reedy
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web