473,387 Members | 1,529 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,387 software developers and data experts.

list manipulation

Hello,

I have a list that looks like this:
roadList = ["Motorways","Local","Arterial"]

I want to apply some code so that the output looks like this:
"Motorways;Local;Arterial"

....in other words, I want each item in the list separated by a ';' and
then the whole thing surrounded by quotes.

How can this be done with the LEAST amount of code?

I appreciate your help!
R.D.
Jun 27 '08 #1
6 1480
On Tue, Apr 22, 2008 at 4:55 PM, DataSmash <rd*@new.rr.comwrote:
Hello,

I have a list that looks like this:
roadList = ["Motorways","Local","Arterial"]

I want to apply some code so that the output looks like this:
"Motorways;Local;Arterial"
How can this be done with the LEAST amount of code?
Not sure if it's LEAST amount of code, or the best, but it works.
>>li = ["Motorways","Local","Arterial"]
'\"%s\"' % (''.join(['%s;' % (x,) for x in li]),)
'"Motorways;Local;Arterial;"'
>>>
Jun 27 '08 #2
On Apr 22, 3:55 pm, DataSmash <r...@new.rr.comwrote:
Hello,

I have a list that looks like this:
roadList = ["Motorways","Local","Arterial"]

I want to apply some code so that the output looks like this:
"Motorways;Local;Arterial"

...in other words, I want each item in the list separated by a ';' and
then the whole thing surrounded by quotes.

How can this be done with the LEAST amount of code?

I appreciate your help!
R.D.
Well you could always do something like this:

output = ';'.join(roadList)

Which will put single quotes on the ends. I suppose if you want to be
silly, you could do this:

output = '"%s"' % ';'.join(roadList)

HTH

Mike
Jun 27 '08 #3
Joe & Mike,
Thanks for your input!
R.D.


Jun 27 '08 #4
Joe Riopel wrote:
On Tue, Apr 22, 2008 at 4:55 PM, DataSmash <rd*@new.rr.comwrote:
>Hello,

I have a list that looks like this:
roadList = ["Motorways","Local","Arterial"]

I want to apply some code so that the output looks like this:
"Motorways;Local;Arterial"
How can this be done with the LEAST amount of code?

Not sure if it's LEAST amount of code, or the best, but it works.
It's definitely not the least (see below) and it doesn't work -- it puts
a ; after the last list item.
>
>>>li = ["Motorways","Local","Arterial"]
'\"%s\"' % (''.join(['%s;' % (x,) for x in li]),)
'"Motorways;Local;Arterial;"'
That is littered with redundant punctuation. Removing it:
>>'"%s"' % ''.join('%s;' % x for x in li)
'"Motorways;Local;Arterial;"'

Note: that would need the [] put back for Python < 2.4. But in any case
we can further reduce it like this:
>>'"%s;"' % ';'.join(li)
'"Motorways;Local;Arterial;"'
>>>
Now we have minimal, legible code which works on Python back to 2.1 at
least and is only one thump of the Delete key away from what the OP
asked for.

HTH,
John

Jun 27 '08 #5
Mike Driscoll wrote:
Well you could always do something like this:

output = ';'.join(roadList)

Which will put single quotes on the ends.
No, it doesn't. You are conflating foo and repr(foo).

I suppose if you want to be
silly, you could do this:

output = '"%s"' % ';'.join(roadList)
*IF* your first effort were to put single quotes on the ends
('the-text') then your second effort would certainly produce something
silly ... either '"the-text"' or "'the-text'" depending on which of the
assignment or the join method you imagined was producing the single quotes.
Jun 27 '08 #6
John Machin wrote:
Mike Driscoll wrote:
>Well you could always do something like this:

output = ';'.join(roadList)

Which will put single quotes on the ends.

No, it doesn't. You are conflating foo and repr(foo).

I suppose if you want to be
>silly, you could do this:

output = '"%s"' % ';'.join(roadList)

*IF* your first effort were to put single quotes on the ends
('the-text') then your second effort would certainly produce something
silly ... either '"the-text"' or "'the-text'" depending on which of
the assignment or the join method you imagined was producing the
single quotes.
--
http://mail.python.org/mailman/listinfo/python-list
Well, in IDLE's output, it looked like what the OP wanted and the OP
seemed to find my examples helpful. Besides, you did almost the exact
same thing in your final example.

Mike
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joseph Barron | last post by:
Here is a SIMPLE problem that I'm trying to solve. It works in Netscape 6.2, but IE6 gives ""No such interface supported." Below are page1.htm and page2.htm . In page1.htm, there are two...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
10
by: Ben | last post by:
Hi, I am a newbie with C and am trying to get a simple linked list working for my program. The structure of each linked list stores the char *data and *next referencing to the next link. The...
77
by: Ville Vainio | last post by:
I tried to clear a list today (which I do rather rarely, considering that just doing l = works most of the time) and was shocked, SHOCKED to notice that there is no clear() method. Dicts have it,...
12
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
25
by: PNY | last post by:
Hi there, I am having some trouble with list manipulation and was hoping someone could help me. I have no problem reading in a text file as a list using filename.readlines(). However, I am...
0
by: L'eau Prosper Research | last post by:
Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases new TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set. L'eau Prosper Market...
0
by: L'eau Prosper Research | last post by:
NEW TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set By L'eau Prosper Research Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases...
2
by: dave.dex | last post by:
Hi all, I've been searching the docs like mad and I'm a little new to python so apologies if this is a basic question. I would like to extract the results of the following query into a list -...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.