473,586 Members | 2,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

converting a string into a list and vice versa

54 New Member
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 14760
bartonc
6,596 Recognized Expert Expert
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
Patrick C
54 New Member
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>List en</a></td></tr><tr><td>Acme Packet, Inc.</td><td><a href="http://finance.yahoo.c om/q?s=apkt">APKT</a></td><td\n'], ['align=center>0 .3</td><td\n', 'align=center>< a href=/cc/7/82467.html>List en</a></td></tr><tr><td>Agni co-Eagle Mines Limited</td><td><a href="http://finance.yahoo.c om/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(stuf f)
>>> pattern = re.compile ("[A-Z]+")
>>> findings = re.findall(patt ern, stuff)

When i press return i get....
>>> " ".join(stuf f)
Traceback (most recent call last):
File "<interacti ve 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 Recognized Expert Contributor
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>List en</a></td></tr><tr><td>Acme Packet, Inc.</td><td><a href="http://finance.yahoo.c om/q?s=apkt">APKT</a></td><td\n'], ['align=center>0 .3</td><td\n', 'align=center>< a href=/cc/7/82467.html>List en</a></td></tr><tr><td>Agni co-Eagle Mines Limited</td><td><a href="http://finance.yahoo.c om/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(stuf f)
>>> pattern = re.compile ("[A-Z]+")
>>> findings = re.findall(patt ern, stuff)

When i press return i get....
>>> " ".join(stuf f)
Traceback (most recent call last):
File "<interacti ve 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 Recognized Expert Moderator Specialist
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>List en</a></td></tr><tr><td>Acme Packet, Inc.</td><td><a href="http://finance.yahoo.c om/q?s=apkt">APKT</a></td><td\n'], ['align=center>0 .3</td><td\n', 'align=center>< a href=/cc/7/82467.html>List en</a></td></tr><tr><td>Agni co-Eagle Mines Limited</td><td><a href="http://finance.yahoo.c om/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(stuf f)
>>> pattern = re.compile ("[A-Z]+")
>>> findings = re.findall(patt ern, stuff)

When i press return i get....
>>> " ".join(stuf f)
Traceback (most recent call last):
File "<interacti ve 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>List en</a></td></tr><tr><td>Acme Packet, Inc.</td><td><a href="http://finance.yahoo.c om/q?s=apkt">APKT</a></td><td
align=center>0. 3</td><td
align=center><a href=/cc/7/82467.html>List en</a></td></tr><tr><td>Agni co-Eagle Mines Limited</td><td><a href="http://finance.yahoo.c om/q?s=aem">AEM</a></td><td

>>>
Aug 2 '07 #5
Patrick C
54 New Member
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 Recognized Expert Moderator Specialist
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
1904
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 at: http://dcs.nac.uci.edu/~strombrg/converting-binary.html So far, the page includes a variety of programs written in C or python to do:
19
22719
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 the std::string a pointer and a length and then give the std::string control over the pointer and its content? I'm basically trying to avoid...
7
2407
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... Whats the best way to read in each line and break it up into individual ints?
2
3753
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 to the ID of the parent post. I'm trying to convert this into a tree structure in memory (e.g. XML). I understand this can be done with recursion...
5
3327
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 way of converting a project to have builds for both compact and full. Project properties doesn't seem to help Thanks
7
4706
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 as the following: "Smith said the dog belongs (or did belong) to the secretary, an employee of the company." Then the list contains the...
1
3827
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 windows directory tree to mimic the unix tree as closely as possible, but I don't want duplicate files and other inconsistencies. I have come up with...
9
3121
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? the list into a string? then compare the two? thanks in advance
7
6773
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
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7841
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7965
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3838
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.