Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Detecting repeated subsequences of identical items Date: Thu, 21 Apr 2016 15:37:38 +1000 Lines: 37 Message-ID: References: <571843f9$0$1585$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de CQ1h0L7BA8+TuT2IVO59MwwCdRPgdeVyue7Ue5wdFn1Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'received:209.85.223': 0.03; '21,': 0.07; 'purpose.': 0.07; 'cc:addr:python-list': 0.09; 'thu,': 0.15; '*almost*': 0.16; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'grouped': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Detecting': 0.16; 'wrote:': 0.16; 'have:': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'txt': 0.22; 'split': 0.23; 'header :In-Reply-To:1': 0.24; "i've": 0.25; 'skip:" 20': 0.26; 'message- id:@mail.gmail.com': 0.27; 'work.': 0.30; "can't": 0.32; "d'aprano": 0.33; 'steven': 0.33; 'received:google.com': 0.35; 'done': 0.35; 'quite': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'no,': 0.38; 'received:209': 0.38; 'chrisa': 0.84; 'or:': 0.84; 'to:none': 0.91; 'abc': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=Z8FBr+NpJ3EzSBdh9yMEUVf3c2QZ99iwkRBxlzEfoo4=; b=QGO/LrBJEqMtPLYhCoW91VISYFndTpF62lWDEE9YCtXYU7ACbZUjif6ccnnWc7h9Xk WPj3aLWWvYcBeW2xsv7injiKof95LSqQro+rwkYVvcIvxs6oaTfGbGOHkVGqt96mSjJb uk33D//Hz9tZzwDre9jLJx83lLBMLL/F4P8ixAXaBe69w7WmMMjoGL30h5UvH0SNXrec F2faJLJHfhi12jTEr/ji7QZrZ2i5FOGjaE+cnQ7S87/rfBkecHHPrJ31dWAixhQMzC2L dPDfOf4sxssGJi8duhsb2XNszMurKypbVB/lr56EARuawHExG5HGNc2vjTIdOGXgpUdt 2VeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=Z8FBr+NpJ3EzSBdh9yMEUVf3c2QZ99iwkRBxlzEfoo4=; b=UEy2HtDrlJqyeEpTzPSWM3hz1LSPvFmOOjef7Taah+yPfY1NxBOQr7gr3Bs164jt2I t7iR5+iRalGz0EfD72F/ntjqnDOw31vDuv4vf6HNdmmOtfoCTwmTHREInEzJiKXrG1nu KOm9K1tVQ77yhnLV6+m7YL9AK/pon+Xgozngmdg3hBMxg4A34aHLjL/EJBZSUZl1uZ/a sJ73nGMakONfyJtqte3ZWkmYmGdZrCAlTAXW1kHZKKDh6+PDdQoxoYAOerzsl/8VTqNV YgwhHNGEmvDsXvJgDC5wQ6qwFRank+1Obvm2DE0E0kTK0pfGjzsoAxrwWZrZH+iPuIB5 3ISQ== X-Gm-Message-State: AOPr4FUr9yyGrGK9DX6nuL2McxIkb49vwaQq4Jm9SDL6uRKPmmJXjaE5713Av3ANDZeSyVIv6L1OV8G5zOSk0A== X-Received: by 10.107.18.232 with SMTP id 101mr1098299ios.157.1461217059038; Wed, 20 Apr 2016 22:37:39 -0700 (PDT) In-Reply-To: <571843f9$0$1585$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <571843f9$0$1585$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:107436 On Thu, Apr 21, 2016 at 1:07 PM, Steven D'Aprano wrote: > Now I want to group subsequences. For example, I have: > > "ABCABCABCDEABCDEFABCABCABCB" > > and I want to group it into repeating subsequences. I can see two ways to > group it: > > ABC ABC ABCDE ABCDE F ABC ABC ABC B > > or: > > ABC ABC ABC D E A B C D E F ABC ABC ABC B Interesting. I've *almost* managed to (ab)use re.split for this purpose. A one-step solution can be done with re.match: >>> txt = "ABCABCABCDEABCDEFABCABCABCB" >>> re.match(r'(.+)\1+', txt) <_sre.SRE_Match object; span=(0, 9), match='ABCABCABC'> But split then returns only the grouped part: >>> re.split(r'(.+)\1+', txt) ['', 'ABC', 'DEABCDEF', 'ABC', 'B'] or *all* the grouped parts: >>> re.split(r'((.+)\2+)', txt) ['', 'ABCABCABC', 'ABC', 'DEABCDEF', 'ABCABCABC', 'ABC', 'B'] There's definitely a partial solution happening here, but I can't quite make it work. And no, I don't know if there's a standard name for it. ChrisA