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?
6 14745
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?
-
>>> returnedlist = list("abcdef")
-
>>> returnedlist
-
['a', 'b', 'c', 'd', 'e', 'f']
-
>>> " ".join(returnedlist)
-
'a b c d e f'
-
>>> " !Wow! ".join(returnedlist)
-
'a !Wow! b !Wow! c !Wow! d !Wow! e !Wow! f'
-
>>>
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
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. 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. - strList = []
-
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']]
-
for item in stuff:
-
for i in item:
-
strList.append(i)
-
-
outStr = " ".join(strList)
-
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
>>>
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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......
|
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...
|
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...
|
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...
|
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...
|
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?...
|
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!
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
| |