How to parse a string completely into a list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • john.ford@colorado.edu

    How to parse a string completely into a list

    I want to take a long alpha-numeric string with \n and white-space and
    place ALL elements of the string (even individual parts of a long
    white-space) into separate list elements. The most common way I've
    seen this performed is with the split() function, however I don't
    believe that it has the power to do what I am looking for.
    Any suggestions?
    thanks
  • Chris Rebert

    #2
    Re: How to parse a string completely into a list

    On Wed, Sep 24, 2008 at 8:30 PM, <john.ford@colo rado.eduwrote:
    I want to take a long alpha-numeric string with \n and white-space and
    place ALL elements of the string (even individual parts of a long
    white-space) into separate list elements. The most common way I've
    seen this performed is with the split() function, however I don't
    believe that it has the power to do what I am looking for.
    Any suggestions?
    thanks
    --

    >
    Could you please define exactly what you mean by "elements" of a string?

    If you mean characters, then just use list():
    >>list(" \n \t abc")
    [' ', ' ', '\n', ' ', '\t', ' ', 'a', 'b', 'c']

    Regards,
    Chris

    --
    Follow the path of the Iguana...

    Comment

    • john.ford@colorado.edu

      #3
      Re: How to parse a string completely into a list

      On Sep 24, 9:44 pm, "Chris Rebert" <c...@rebertia. comwrote:
      On Wed, Sep 24, 2008 at 8:30 PM,  <john.f...@colo rado.eduwrote:
      I want to take a long alpha-numeric string with \n and white-space and
      place ALL elements of the string (even individual parts of a long
      white-space) into separate list elements. The most common way I've
      seen this performed is with the split() function, however I don't
      believe that it has the power to do what I am looking for.
      Any suggestions?
      thanks
      --
      http://mail.python.org/mailman/listinfo/python-list
      >
      Could you please define exactly what you mean by "elements" of a string?
      >
      If you mean characters, then just use list():>>list("  \n \t abc")
      >
      [' ', ' ', '\n', ' ', '\t', ' ', 'a', 'b', 'c']
      >
      Regards,
      Chris
      >
      --
      Follow the path of the Iguana...http://rebertia.com
      Worked like a charm.
      kudos!

      Comment

      • Matt Nordhoff

        #4
        Re: How to parse a string completely into a list

        john.ford@color ado.edu wrote:
        On Sep 24, 9:44 pm, "Chris Rebert" <c...@rebertia. comwrote:
        >On Wed, Sep 24, 2008 at 8:30 PM, <john.f...@colo rado.eduwrote:
        >>I want to take a long alpha-numeric string with \n and white-space and
        >>place ALL elements of the string (even individual parts of a long
        >>white-space) into separate list elements. The most common way I've
        >>seen this performed is with the split() function, however I don't
        >>believe that it has the power to do what I am looking for.
        >>Any suggestions?
        >>thanks
        >Could you please define exactly what you mean by "elements" of a string?
        >>
        >If you mean characters, then just use list():>>list(" \n \t abc")
        >>
        >[' ', ' ', '\n', ' ', '\t', ' ', 'a', 'b', 'c']
        >>
        >Regards,
        >Chris
        >
        Worked like a charm.
        kudos!
        Why do you need to convert it to a list? Strings are sequences, so you
        can do things like slice them or iterate through them by character:
        >>for character in "foo":
        .... print character
        ....
        f
        o
        o
        >>>
        --

        Comment

        • john.ford@colorado.edu

          #5
          Re: How to parse a string completely into a list

          On Sep 24, 10:12 pm, Matt Nordhoff <mnordh...@matt nordhoff.comwro te:
          john.f...@color ado.edu wrote:
          On Sep 24, 9:44 pm, "Chris Rebert" <c...@rebertia. comwrote:
          On Wed, Sep 24, 2008 at 8:30 PM,  <john.f...@colo rado.eduwrote:
          >I want to take a long alpha-numeric string with \n and white-space and
          >place ALL elements of the string (even individual parts of a long
          >white-space) into separate list elements. The most common way I've
          >seen this performed is with the split() function, however I don't
          >believe that it has the power to do what I am looking for.
          >Any suggestions?
          >thanks
          Could you please define exactly what you mean by "elements" of a string?
          >
          If you mean characters, then just use list():>>list("  \n \t abc")
          >
          [' ', ' ', '\n', ' ', '\t', ' ', 'a', 'b', 'c']
          >
          Regards,
          Chris
          >
          Worked like a charm.
          kudos!
          >
          Why do you need to convert it to a list? Strings are sequences, so you
          can do things like slice them or iterate through them by character:
          >
          >for character in "foo":
          >
          ...     print character
          ...
          f
          o
          o
          >
          --
          The string draws a map that I then want to be able to traverse
          through. If I can count through the individual characters of a list I
          can create an x-y coordinate plane for navigation.

          Comment

          • Tim Roberts

            #6
            Re: How to parse a string completely into a list

            john.ford@color ado.edu wrote:
            >
            >The string draws a map that I then want to be able to traverse
            >through. If I can count through the individual characters of a list I
            >can create an x-y coordinate plane for navigation.
            Well, the point Matt was making is that traversing through a list and
            traversing through a string are the same.

            # Given this:
            s = 'abcde'
            l = ['a','b','c','d' ,'e']

            # These are identical:
            for ch in s:
            pass
            for ch in l:
            pass

            # And these are identical:
            print s[3]
            print l[3]

            Slicing is identical. Subsetting is identical. The only difference is
            that I can change an element of the list.
            --
            Tim Roberts, timr@probo.com
            Providenza & Boekelheide, Inc.

            Comment

            • Bruno Desthuilliers

              #7
              Re: How to parse a string completely into a list

              john.ford@color ado.edu a écrit :
              I want to take a long alpha-numeric string with \n and white-space and
              place ALL elements of the string (even individual parts of a long
              white-space) into separate list elements. The most common way I've
              seen this performed is with the split() function, however I don't
              believe that it has the power to do what I am looking for.
              Any suggestions?
              Did you try passing your string to the list() type ?

              Python 2.5.1 (r251:54863, Mar 7 2008, 03:41:45)
              [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
              Type "help", "copyright" , "credits" or "license" for more information.
              >>s = """I want to take a long alpha-numeric string with \n and
              white-space and
              .... place ALL elements of the string (even individual parts of a long
              .... white-space) into separate list elements. The most common way I've
              .... seen this performed is with the split() function, however I don't
              .... believe that it has the power to do what I am looking for.
              .... Any suggestions?
              .... thanks
              .... """
              >>>
              >>s
              "I want to take a long alpha-numeric string with \n and white-space
              and\nplace ALL elements of the string (even individual parts of a
              long\nwhite-space) into separate list elements. The most common way
              I've\nseen this performed is with the split() function, however I
              don't\nbelieve that it has the power to do what I am looking for.\nAny
              suggestions?\nt hanks\n"
              >>list(s)
              ['I', ' ', 'w', 'a', 'n', 't', ' ', 't', 'o', ' ', 't', 'a', 'k', 'e', '
              ', 'a', ' ', 'l', 'o', 'n', 'g', ' ', 'a', 'l', 'p', 'h', 'a', '-', 'n',
              'u', 'm', 'e', 'r', 'i', 'c', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ',
              'w', 'i', 't', 'h', ' ', '\n', ' ', 'a', 'n', 'd', ' ', 'w', 'h', 'i',
              't', 'e', '-', 's', 'p', 'a', 'c', 'e', ' ', 'a', 'n', 'd', '\n', 'p',
              'l', 'a', 'c', 'e', ' ', 'A', 'L', 'L', ' ', 'e', 'l', 'e', 'm', 'e',
              'n', 't', 's', ' ', 'o', 'f', ' ', 't', 'h', 'e', ' ', 's', 't', 'r',
              'i', 'n', 'g', ' ', '(', 'e', 'v', 'e', 'n', ' ', 'i', 'n', 'd', 'i',
              'v', 'i', 'd', 'u', 'a', 'l', ' ', 'p', 'a', 'r', 't', 's', ' ', 'o',
              'f', ' ', 'a', ' ', 'l', 'o', 'n', 'g', '\n', 'w', 'h', 'i', 't', 'e',
              '-', 's', 'p', 'a', 'c', 'e', ')', ' ', 'i', 'n', 't', 'o', ' ', 's',
              'e', 'p', 'a', 'r', 'a', 't', 'e', ' ', 'l', 'i', 's', 't', ' ', 'e',
              'l', 'e', 'm', 'e', 'n', 't', 's', '.', ' ', 'T', 'h', 'e', ' ', 'm',
              'o', 's', 't', ' ', 'c', 'o', 'm', 'm', 'o', 'n', ' ', 'w', 'a', 'y', '
              ', 'I', "'", 'v', 'e', '\n', 's', 'e', 'e', 'n', ' ', 't', 'h', 'i',
              's', ' ', 'p', 'e', 'r', 'f', 'o', 'r', 'm', 'e', 'd', ' ', 'i', 's', '
              ', 'w', 'i', 't', 'h', ' ', 't', 'h', 'e', ' ', 's', 'p', 'l', 'i', 't',
              '(', ')', ' ', 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n', ',', ' ', 'h',
              'o', 'w', 'e', 'v', 'e', 'r', ' ', 'I', ' ', 'd', 'o', 'n', "'", 't',
              '\n', 'b', 'e', 'l', 'i', 'e', 'v', 'e', ' ', 't', 'h', 'a', 't', ' ',
              'i', 't', ' ', 'h', 'a', 's', ' ', 't', 'h', 'e', ' ', 'p', 'o', 'w',
              'e', 'r', ' ', 't', 'o', ' ', 'd', 'o', ' ', 'w', 'h', 'a', 't', ' ',
              'I', ' ', 'a', 'm', ' ', 'l', 'o', 'o', 'k', 'i', 'n', 'g', ' ', 'f',
              'o', 'r', '.', '\n', 'A', 'n', 'y', ' ', 's', 'u', 'g', 'g', 'e', 's',
              't', 'i', 'o', 'n', 's', '?', '\n', 't', 'h', 'a', 'n', 'k', 's', '\n']
              >>>

              HTH

              Comment

              • john.ford@colorado.edu

                #8
                Re: How to parse a string completely into a list

                On Sep 25, 1:51 am, Tino Wildenhain <t...@wildenhai n.dewrote:
                john.f...@color ado.edu wrote:
                On Sep 24, 10:12 pm, Matt Nordhoff <mnordh...@matt nordhoff.comwro te:
                john.f...@color ado.edu wrote:
                >On Sep 24, 9:44 pm, "Chris Rebert" <c...@rebertia. comwrote:
                ....
                >>Could you please define exactly what you mean by "elements" of a string?
                >>If you mean characters, then just use list():>>list("  \n \t abc")
                >>[' ', ' ', '\n', ' ', '\t', ' ', 'a', 'b', 'c']
                >>Regards,
                >>Chris
                >Worked like a charm.
                >kudos!
                Why do you need to convert it to a list? Strings are sequences, so you
                can do things like slice them or iterate through them by character:
                >
                >>>for character in "foo":
                ...     print character
                ...
                f
                o
                o
                >
                --
                >
                The string draws a map that I then want to be able to traverse
                through. If I can count through the individual characters of a list I
                can create an x-y coordinate plane for navigation.
                >
                You can 'count' (whatever that means) equally in strings as you do in
                lists. As said above, they behave exactly the same. Just strings
                are imutable - e.g. you can't change individual parts of them.
                >
                Tino
                >>
                 smime.p7s
                4KViewDownload
                Ahh, but I forgot to mention that I have to mark the path I took in
                the string. So using list() and then join() are my best options.

                Comment

                Working...