471,344 Members | 1,006 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 software developers and data experts.

converting a string into a list and vice versa

If I have a loop that yields something like:

returnedlist = ['a', 'b', 'c',
'd', 'e', 'f']

how can I make the contents of returnedlist into a string called NewString that outputs something like

NewString= (a b c
d e f)

Same question for the reverse request.

Lastly, in regex can lists be manipulated, or is it a string-only kind of party?
Jul 27 '07 #1
6 14651
bartonc
6,596 Expert 4TB
If I have a loop that yields something like:

returnedlist = ['a', 'b', 'c',
'd', 'e', 'f']

how can I make the contents of returnedlist into a string called NewString that outputs something like

NewString= (a b c
d e f)

Same question for the reverse request.

Lastly, in regex can lists be manipulated, or is it a string-only kind of party?
Expand|Select|Wrap|Line Numbers
  1. >>> returnedlist = list("abcdef")
  2. >>> returnedlist
  3. ['a', 'b', 'c', 'd', 'e', 'f']
  4. >>> " ".join(returnedlist)
  5. 'a b c d e f'
  6. >>> " !Wow! ".join(returnedlist)
  7. 'a !Wow! b !Wow! c !Wow! d !Wow! e !Wow! f'
  8. >>> 
Jul 27 '07 #2
Okay, that was helpful but maybe i'm doing it wrong. Here's my real world application....
I have a script that when it prints (on the screen) it returns something that looks like...

[['name=Keywords\n', 'xmlns=""\n'], ['align=center>0.08</td><td\n', 'align=center><a href=/cc/3/83683.html>Listen</a></td></tr><tr><td>Acme Packet, Inc.</td><td><a href="http://finance.yahoo.com/q?s=apkt">APKT</a></td><td\n'], ['align=center>0.3</td><td\n', 'align=center><a href=/cc/7/82467.html>Listen</a></td></tr><tr><td>Agnico-Eagle Mines Limited</td><td><a href="http://finance.yahoo.com/q?s=aem">AEM</a></td><td\n'],

etc etc

that is when i type print stuff

But I want to do a few basic regex on it, and I thought my problem was that stuff is a list, not a string, so I tried this....

>>> " ".join(stuff)
>>> pattern = re.compile ("[A-Z]+")
>>> findings = re.findall(pattern, stuff)

When i press return i get....
>>> " ".join(stuff)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: sequence item 0: expected string, list found


Any idea why this is happening and what I can do to avoid it?

thanks
pc
Aug 1 '07 #3
ilikepython
844 Expert 512MB
Okay, that was helpful but maybe i'm doing it wrong. Here's my real world application....
I have a script that when it prints (on the screen) it returns something that looks like...

[['name=Keywords\n', 'xmlns=""\n'], ['align=center>0.08</td><td\n', 'align=center><a href=/cc/3/83683.html>Listen</a></td></tr><tr><td>Acme Packet, Inc.</td><td><a href="http://finance.yahoo.com/q?s=apkt">APKT</a></td><td\n'], ['align=center>0.3</td><td\n', 'align=center><a href=/cc/7/82467.html>Listen</a></td></tr><tr><td>Agnico-Eagle Mines Limited</td><td><a href="http://finance.yahoo.com/q?s=aem">AEM</a></td><td\n'],

etc etc

that is when i type print stuff

But I want to do a few basic regex on it, and I thought my problem was that stuff is a list, not a string, so I tried this....

>>> " ".join(stuff)
>>> pattern = re.compile ("[A-Z]+")
>>> findings = re.findall(pattern, stuff)

When i press return i get....
>>> " ".join(stuff)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: sequence item 0: expected string, list found


Any idea why this is happening and what I can do to avoid it?

thanks
pc
The join function doesn't change the list in place. It return a string seperate from the list. You call join but never set the return value to a variable.
Expand|Select|Wrap|Line Numbers
  1. stuff = "".join(stuff)
  2.  
Aug 1 '07 #4
bvdet
2,851 Expert Mod 2GB
Okay, that was helpful but maybe i'm doing it wrong. Here's my real world application....
I have a script that when it prints (on the screen) it returns something that looks like...

[['name=Keywords\n', 'xmlns=""\n'], ['align=center>0.08</td><td\n', 'align=center><a href=/cc/3/83683.html>Listen</a></td></tr><tr><td>Acme Packet, Inc.</td><td><a href="http://finance.yahoo.com/q?s=apkt">APKT</a></td><td\n'], ['align=center>0.3</td><td\n', 'align=center><a href=/cc/7/82467.html>Listen</a></td></tr><tr><td>Agnico-Eagle Mines Limited</td><td><a href="http://finance.yahoo.com/q?s=aem">AEM</a></td><td\n'],

etc etc

that is when i type print stuff

But I want to do a few basic regex on it, and I thought my problem was that stuff is a list, not a string, so I tried this....

>>> " ".join(stuff)
>>> pattern = re.compile ("[A-Z]+")
>>> findings = re.findall(pattern, stuff)

When i press return i get....
>>> " ".join(stuff)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: sequence item 0: expected string, list found


Any idea why this is happening and what I can do to avoid it?

thanks
pc
'stuff' is a list of lists. You can only join a list of strings.
Expand|Select|Wrap|Line Numbers
  1. strList = []
  2. stuff = [['name=Keywords\n', 'xmlns=""\n'], ['align=center>0.08</td><td\n', 'align=center><a href=/cc/3/83683.html>Listen</a></td></tr><tr><td>Acme Packet, Inc.</td><td><a href="http://finance.yahoo.com/q?s=apkt">APKT</a></td><td\n'], ['align=center>0.3</td><td\n', 'align=center><a href=/cc/7/82467.html>Listen</a></td></tr><tr><td>Agnico-Eagle Mines Limited</td><td><a href="http://finance.yahoo.com/q?s=aem">AEM</a></td><td\n']]
  3. for item in stuff:
  4.     for i in item:
  5.         strList.append(i)
  6.  
  7. outStr = " ".join(strList)
  8. print outStr
Output:
>>> name=Keywords
xmlns=""
align=center>0.08</td><td
align=center><a href=/cc/3/83683.html>Listen</a></td></tr><tr><td>Acme Packet, Inc.</td><td><a href="http://finance.yahoo.com/q?s=apkt">APKT</a></td><td
align=center>0.3</td><td
align=center><a href=/cc/7/82467.html>Listen</a></td></tr><tr><td>Agnico-Eagle Mines Limited</td><td><a href="http://finance.yahoo.com/q?s=aem">AEM</a></td><td

>>>
Aug 2 '07 #5
BVDET,

Your solution works perfect. I'd like to just try and reitterate what it does in english to make sure I have a firm grasp, so that I don't ask the same question in the future.

Essentially my scrape returned a lists of lists. But I wanted a list of strings.

So the first thing you did is create a new list called strList.

Then the FOR loop had it go over each item in the list of lists and put them into strList.

When it put it into strList, it was put in as a string by default (?).


From there, because the elemants in the original list of lists were now saved as a list of strings, I could do join function.

Right...?

thanks
pc
Aug 3 '07 #6
bvdet
2,851 Expert Mod 2GB
BVDET,

Your solution works perfect. I'd like to just try and reitterate what it does in english to make sure I have a firm grasp, so that I don't ask the same question in the future.

Essentially my scrape returned a lists of lists. But I wanted a list of strings.

So the first thing you did is create a new list called strList.

Then the FOR loop had it go over each item in the list of lists and put them into strList.

When it put it into strList, it was put in as a string by default (?).


From there, because the elemants in the original list of lists were now saved as a list of strings, I could do join function.

Right...?

thanks
pc
Yes. The nested lists contained only strings, and each element remained a string when appended to strList.
Aug 3 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Dan Stromberg | last post: by
19 posts views Thread by Espen Ruud Schultz | last post: by
7 posts views Thread by Jus! | last post: by
2 posts views Thread by Oleg Ogurok | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.