473,320 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 14745
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

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

Similar topics

0
by: Dan Stromberg | last post by:
I've written up a page about how to convert native binary data to another platform's native binary data, as I did some fortran data conversions for a client. The programs and documentation are...
19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
7
by: Jus! | last post by:
Hi. I am reading bits(1's & 0's) from a file and i wa wondering what is the most efficient method of converting these strings to individual int's? eg. File contains: 110001 010011 etc......
2
by: Oleg Ogurok | last post by:
Hi all, I'm looking for a fast algorithm to do the following: A DataTable has the following columns: ID, ParentID, Title, Body, etc. It represents webforum conversation threads. ParentID points...
5
by: bob | last post by:
Hi Using 2003 - targeting the compact framework (c#), but would like to do most development using the full.net (manually leaving out stuff not in the compact framework). Q. Trying to find a...
7
by: Karch | last post by:
I need to find the fastest way in terms of storage and searching to determine if a given string contains one of a member of a list of strings. So, think of it in terms of this: I have a string such...
1
RRick
by: RRick | last post by:
I have a unix C++ project that needs to be converted over to windows visual studio. I'm not sure of the exact version of VS, but it's a recent version, probabIy 2003 or 2005. I would like the...
9
by: victory2006 | last post by:
I need help converting a list into a string ex/ i want to compare this, "110"(string) to this (list) to see if they are identical. So, how would you do this? convert the string into a list?...
7
by: victory2006 | last post by:
like we have an integer value of : 9 and i need to convert it to binary numbers and vice versa, thanks!
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.