Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:" ': 0.03; 'python': 0.07; '"done"': 0.09; 'append': 0.09; 'from:addr:python': 0.09; 'string)': 0.09; 'subject:string': 0.09; '>>>': 0.12; 'wrote:': 0.14; 'subject:python': 0.15; '-1:': 0.16; 'delimited': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'reply-to:addr:python-list': 0.16; 'w/o': 0.16; 'subject:list': 0.22; 'code': 0.22; 'header:In-Reply- To:1': 0.22; 'received:84': 0.25; 'thanks': 0.29; 'string': 0.29; 'list': 0.30; 'to:addr:python-list': 0.32; 'things': 0.33; 'print': 0.35; 'header:User-Agent:1': 0.35; 'reply- to:addr:python.org': 0.35; 'list.': 0.35; 'doing': 0.36; 'some': 0.37; 'it?': 0.37; 'skip:" 20': 0.38; 'end': 0.39; 'to:addr:python.org': 0.39; 'how': 0.39; 'experts': 0.64; 'below.': 0.64; 'reply-to:no real name:2**0': 0.72; 'header:Reply- To:1': 0.72; '***': 0.73; 'subject:best': 0.93 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtIIAChrzU1UXebj/2dsb2JhbACYCI4Bd4hwvHuGFQSUNYpE Date: Fri, 13 May 2011 18:36:53 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 MIME-Version: 1.0 To: python-list@python.org Subject: Re: How best to convert a string "list" to a python list References: <2efc3a05-22e5-46db-bd22-c95221bbd7b2@k16g2000yqm.googlegroups.com> In-Reply-To: <2efc3a05-22e5-46db-bd22-c95221bbd7b2@k16g2000yqm.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 34 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305308221 news.xs4all.nl 41102 [::ffff:82.94.164.166]:47175 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5315 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']