Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > convert string containing list to list (or tuple) type

Reply
Thread Tools

convert string containing list to list (or tuple) type

 
 
Poppy
Guest
Posts: n/a
 
      05-30-2008
I'm reading from a database a column that has a list of codes (comma
seperated). When I read in the list I have a single value, see code sample
below values for a, b, and c. These represent possible values in my
database. I need to loop through each value so I can expand my data from
this compressed view.

My code below works and creates my desired output but I believe there must
be a better way this is very messy. My messy function that I'd like to
replace is lst_codes(codes). Any alternative suggestions?

this is what I begin with
a = ',P,'
b = ',I,G,AQ,ET,K,BF,'
c = ',DZ,'
this is what I want (lists or tuples are fine)
['P']
['I', 'G', 'AQ', 'ET', 'K', 'BF']
['DZ']


def lst_codes(codes):
""" turn a string of comma seperated codes into a real list object """
i = 0
lstD = []
while i < len(codes):
a = codes[i]
b = ","
if (i + 1) < len(codes):
b = codes[i + 1]
i = i + 1
else:
b = ","

if b <> ",":
lstD.append(a + b)
i = i + 2
else:
lstD.append(a)
i = i + 1
return lstD


a = ',P,'
b = ',I,G,AQ,ET,K,BF,'
c = ',DZ,'

for ea in (a,b,c):
print lst_codes(ea.strip(","))


 
Reply With Quote
 
 
 
 
Poppy
Guest
Posts: n/a
 
      05-30-2008
Arrgh. One of those days where I find an answer just after posting. I spend
hours on the code below only to find I don't know how to use split to it's
fullest.

>>> b.strip(",").split(",")

['I', 'G', 'AQ', 'ET', 'K', 'BF']

"Poppy" <(E-Mail Removed)> wrote in message
news:g1p15p$4jo$(E-Mail Removed)...
> I'm reading from a database a column that has a list of codes (comma
> seperated). When I read in the list I have a single value, see code sample
> below values for a, b, and c. These represent possible values in my
> database. I need to loop through each value so I can expand my data from
> this compressed view.
>
> My code below works and creates my desired output but I believe there must
> be a better way this is very messy. My messy function that I'd like to
> replace is lst_codes(codes). Any alternative suggestions?
>
> this is what I begin with
> a = ',P,'
> b = ',I,G,AQ,ET,K,BF,'
> c = ',DZ,'
> this is what I want (lists or tuples are fine)
> ['P']
> ['I', 'G', 'AQ', 'ET', 'K', 'BF']
> ['DZ']
>
>
> def lst_codes(codes):
> """ turn a string of comma seperated codes into a real list object """
> i = 0
> lstD = []
> while i < len(codes):
> a = codes[i]
> b = ","
> if (i + 1) < len(codes):
> b = codes[i + 1]
> i = i + 1
> else:
> b = ","
>
> if b <> ",":
> lstD.append(a + b)
> i = i + 2
> else:
> lstD.append(a)
> i = i + 1
> return lstD
>
>
> a = ',P,'
> b = ',I,G,AQ,ET,K,BF,'
> c = ',DZ,'
>
> for ea in (a,b,c):
> print lst_codes(ea.strip(","))
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initializing compound type containing opaque type Nate Eldredge C Programming 23 10-13-2008 05:13 AM
convert a line with whitespaces into a string containing lin Vellingiri Arul Ruby 3 09-19-2007 10:00 AM
Convert string data type to char type ? tvn007@hotmail.com C++ 6 10-20-2005 02:22 PM
convert type string to type guid guoqi zheng ASP .Net 1 07-02-2005 02:44 PM
Convert String Containing Hex Values cp Perl Misc 10 11-01-2004 07:52 PM



Advertisments