Compress a string

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

    Compress a string

    Hi guys,

    I'm trying to compress a string.
    E.g:
    "AAAABBBC" -"ABC"

    The code I have so far feels like it could be made clearer and more
    succinct, but a solution is currently escaping me.


    def compress_str(st r):
    new_str = ""
    for i, c in enumerate(str):
    try:
    if c != str[i+1]:
    new_str += c
    except IndexError:
    new_str += c
    return new_str


    Cheers
    Matt
    --
    --

  • Salvatore DI DI0

    #2
    Re: Compress a string

    Try this

    t = set("aaaaaaaaaa aabbbbbbbbbbccc cccccc")
    list(t)

    Regards

    Salvatore


    "Matt Porter" <mabbikeel@gmai l.coma écrit dans le message de news:
    mailman.1315.12 11133997.12834. py...ist@python.org...
    Hi guys,
    >
    I'm trying to compress a string.
    E.g:
    "AAAABBBC" -"ABC"
    >
    The code I have so far feels like it could be made clearer and more
    succinct, but a solution is currently escaping me.
    >
    >
    def compress_str(st r):
    new_str = ""
    for i, c in enumerate(str):
    try:
    if c != str[i+1]:
    new_str += c
    except IndexError:
    new_str += c
    return new_str
    >
    >
    Cheers
    Matt
    --
    --
    >

    Comment

    • Arnaud Delobelle

      #3
      Re: Compress a string

      "Matt Porter" <mabbikeel@gmai l.comwrites:
      Hi guys,
      >
      I'm trying to compress a string.
      E.g:
      "AAAABBBC" -"ABC"
      >
      The code I have so far feels like it could be made clearer and more
      succinct, but a solution is currently escaping me.
      >
      >
      def compress_str(st r):
      new_str = ""
      for i, c in enumerate(str):
      try:
      if c != str[i+1]:
      new_str += c
      except IndexError:
      new_str += c
      return new_str
      >
      >
      Cheers
      Matt
      --
      --
      >>string = 'sssssssssspppp pppaaaaaaam'
      >>''.join(x for x, y in zip(string, '\0'+string) if x != y)
      'spam'

      HTH

      PS: I keep seeing problems on this list whose solution seems to
      involve 'window' iterating over a sequence. E.g.
      >>list(window(' eggs', 2))
      [('e', 'g'), ('g', 'g'), ('g', 's')]

      --
      Arnaud

      Comment

      • Peter Otten

        #4
        Re: Compress a string

        Matt Porter wrote:
        I'm trying to compress a string.
        E.g:
        "AAAABBBC" -"ABC"
        Two more:
        >>from itertools import groupby
        >>"".join(k for k, g in groupby("aaaaaa bbbbbbbbbbccccc c"))
        'abc'
        >>import re
        >>re.compile(r" (.)\1*").sub(r" \1", "aaaaaaabbbbccc ccccc")
        'abc'

        Peter

        Comment

        • Marc Christiansen

          #5
          Re: Compress a string

          Matt Porter <mabbikeel@gmai l.comwrote:
          Hi guys,
          >
          I'm trying to compress a string.
          E.g:
          "AAAABBBC" -"ABC"
          You mean like this?
          >>''.join(c for c, _ in itertools.group by("AAAABBBCAAD CASS"))
          'ABCADCAS'

          HTH
          Marc

          Comment

          • Matt Porter

            #6
            Re: Compress a string

            On Sun, 18 May 2008 20:30:57 +0100, Peter Otten <__peter__@web. dewrote:
            Matt Porter wrote:
            >
            >I'm trying to compress a string.
            >E.g:
            > "AAAABBBC" -"ABC"
            >
            Two more:
            >
            >>>from itertools import groupby
            >>>"".join(k for k, g in groupby("aaaaaa bbbbbbbbbbccccc c"))
            'abc'
            >
            >>>import re
            >>>re.compile(r "(.)\1*").sub(r "\1", "aaaaaaabbbbccc ccccc")
            'abc'
            >
            Brilliant - I was trying to figure out how to do this with the re
            capabilities.

            Thanks to everyone


            --
            --

            Comment

            • inhahe

              #7
              Re: Compress a string

              i see lots of neat one-liner solutions but just for the sake of argument:

              def compress_str(st r):
              new_str = ""
              lc = ""
              for c in str:
              if c != lc: new_str.append( c)
              return new_str


              "Matt Porter" <mabbikeel@gmai l.comwrote in message
              news:mailman.13 15.1211133997.1 2834.python-list@python.org ...
              Hi guys,
              >
              I'm trying to compress a string.
              E.g:
              "AAAABBBC" -"ABC"
              >
              The code I have so far feels like it could be made clearer and more
              succinct, but a solution is currently escaping me.
              >
              >
              >
              >
              Cheers
              Matt
              --
              --
              >

              Comment

              • Bjoern Schliessmann

                #8
                Re: Compress a string

                inhahe wrote:
                i see lots of neat one-liner solutions but just for the sake of
                argument:
                >
                def compress_str(st r):
                new_str = ""
                lc = ""
                for c in str:
                if c != lc: new_str.append( c)
                return new_str
                Please test before posting.
                >>compress_str( "AAAABBBBCCCCC" )
                Traceback (most recent call last):
                File "<stdin>", line 1, in <module>
                File "<stdin>", line 5, in compress_str
                AttributeError: 'str' object has no attribute 'append'

                Regards,


                Björn

                --
                BOFH excuse #48:

                bad ether in the cables

                Comment

                • John Salerno

                  #9
                  Re: Compress a string

                  On Sun, 18 May 2008 19:06:10 +0100
                  "Matt Porter" <mabbikeel@gmai l.comwrote:
                  Hi guys,
                  >
                  I'm trying to compress a string.
                  E.g:
                  "AAAABBBC" -"ABC"
                  Not that you need help anymore, but I decided to give it a try. Not as elegant as the one- and two-liners, but somewhat concise I guess.

                  def compress(s):
                  new = [s[:1]]

                  for c in s[1:]:
                  if c not in new:
                  new.append(c)
                  return ''.join(new)

                  Comment

                  • John Salerno

                    #10
                    Re: Compress a string

                    On Tue, 20 May 2008 00:09:14 -0400
                    John Salerno <johnjsal@NOSPA Mgmail.comwrote :
                    Not that you need help anymore, but I decided to give it a try. Not as elegant as the one- and two-liners, but somewhat concise I guess.
                    Whoops! Could be cleaner! :)

                    def compress(s):
                    new = []

                    for c in s:
                    if c not in new:
                    new.append(c)
                    return ''.join(new)


                    No, wait! I can do better!

                    def compress(s):
                    new = []
                    [new.append(c) for c in s if c not in new]
                    return ''.join(new)

                    Wow, list comprehensions are cool.

                    Comment

                    • Marc 'BlackJack' Rintsch

                      #11
                      Re: Compress a string

                      On Tue, 20 May 2008 00:38:57 -0400, John Salerno wrote:
                      def compress(s):
                      new = []
                      >
                      for c in s:
                      if c not in new:
                      new.append(c)
                      return ''.join(new)
                      >
                      >
                      No, wait! I can do better!
                      >
                      def compress(s):
                      new = []
                      [new.append(c) for c in s if c not in new] return ''.join(new)
                      >
                      Wow, list comprehensions are cool.
                      And it's a misuse of list comprehension here IMHO because they are meant to
                      build lists, not to cram a ``for``/``if`` loop into a one liner. You are
                      building a list full of `None` objects, just to throw it away.

                      Ciao,
                      Marc 'BlackJack' Rintsch

                      Comment

                      • Bruno Desthuilliers

                        #12
                        Re: Compress a string

                        Salvatore DI DI0 a écrit :
                        (top-post corrected - Salvatore, please, don't top-post)
                        "Matt Porter" <mabbikeel@gmai l.coma écrit dans le message de news:
                        mailman.1315.12 11133997.12834. py...ist@python.org...
                        >Hi guys,
                        >>
                        >I'm trying to compress a string.
                        >E.g:
                        > "AAAABBBC" -"ABC"
                        Try this
                        >
                        t = set("aaaaaaaaaa aabbbbbbbbbbccc cccccc")
                        list(t)
                        Won't keep the ordering.


                        Comment

                        • Bruno Desthuilliers

                          #13
                          Re: Compress a string

                          Matt Porter a écrit :
                          Hi guys,
                          >
                          I'm trying to compress a string.
                          E.g:
                          "AAAABBBC" -"ABC"
                          >
                          The code I have so far feels like it could be made clearer and more
                          succinct, but a solution is currently escaping me.
                          >
                          >
                          def compress_str(st r):
                          using 'str' as an indentifier will shadow the builtin str type.
                          new_str = ""
                          for i, c in enumerate(str):
                          try:
                          if c != str[i+1]:
                          new_str += c
                          except IndexError:
                          new_str += c
                          return new_str
                          >
                          >
                          Now everyone gave smart solutions, may I suggest the stupidier possible one:

                          def compress_string (astring):
                          compressed = []
                          for c in astring:
                          if c not in compressed:
                          compressed.appe nd(c)
                          return ''.join(compres sed)

                          Not elegant, but at least very clear.

                          Comment

                          Working...