list manipulation

D

DataSmash

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.
 
J

Joe Riopel

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;"'
 
M

Mike Driscoll

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
 
J

John Machin

Joe said:
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:
'"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:

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
 
J

John Machin

Mike said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,798
Messages
2,569,651
Members
45,384
Latest member
GOLDY

Latest Threads

Top