list manipulation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DataSmash

    list manipulation

    Hello,

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

    I want to apply some code so that the output looks like this:
    "Motorways;Loca l;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.
  • Joe Riopel

    #2
    Re: list manipulation

    On Tue, Apr 22, 2008 at 4:55 PM, DataSmash <rdh@new.rr.com wrote:
    Hello,
    >
    I have a list that looks like this:
    roadList = ["Motorways","Lo cal","Arterial "]
    >
    I want to apply some code so that the output looks like this:
    "Motorways;Loca l;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","Lo cal","Arterial "]
    >>'\"%s\"' % (''.join(['%s;' % (x,) for x in li]),)
    '"Motorways;Loc al;Arterial;"'
    >>>

    Comment

    • Mike Driscoll

      #3
      Re: list manipulation

      On Apr 22, 3:55 pm, DataSmash <r...@new.rr.co mwrote:
      Hello,
      >
      I have a list that looks like this:
      roadList = ["Motorways","Lo cal","Arterial "]
      >
      I want to apply some code so that the output looks like this:
      "Motorways;Loca l;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(roadLi st)

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

      output = '"%s"' % ';'.join(roadLi st)

      HTH

      Mike

      Comment

      • DataSmash

        #4
        Re: list manipulation

        Joe & Mike,
        Thanks for your input!
        R.D.




        Comment

        • John Machin

          #5
          Re: list manipulation

          Joe Riopel wrote:
          On Tue, Apr 22, 2008 at 4:55 PM, DataSmash <rdh@new.rr.com wrote:
          >Hello,
          >>
          > I have a list that looks like this:
          > roadList = ["Motorways","Lo cal","Arterial "]
          >>
          > I want to apply some code so that the output looks like this:
          > "Motorways;Loca l;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","Lo cal","Arterial "]
          >>>'\"%s\"' % (''.join(['%s;' % (x,) for x in li]),)
          '"Motorways;Loc al;Arterial;"'
          That is littered with redundant punctuation. Removing it:
          >>'"%s"' % ''.join('%s;' % x for x in li)
          '"Motorways;Loc al;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;Loc al;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



          Comment

          • John Machin

            #6
            Re: list manipulation

            Mike Driscoll wrote:
            Well you could always do something like this:
            >
            output = ';'.join(roadLi st)
            >
            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(roadLi st)
            *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.

            Comment

            • Mike Driscoll

              #7
              Re: list manipulation

              John Machin wrote:
              Mike Driscoll wrote:
              >
              >Well you could always do something like this:
              >>
              >output = ';'.join(roadLi st)
              >>
              >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(roadLi st)
              >
              *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

              Comment

              Working...