Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Oscar Benjamin Newsgroups: comp.lang.python Subject: Re: Detecting repeated subsequences of identical items Date: Thu, 21 Apr 2016 09:53:04 +0100 Lines: 43 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 CD224aKjKBPJXuABdzXDIg+HE5M37NXmbcqWBto88dDA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python)': 0.05; 'repeated': 0.07; 'stack.': 0.07; 'cc:addr:python-list': 0.09; '2016': 0.16; 'cc:name:python list': 0.16; 'itertools': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sequence.': 0.16; 'subject:Detecting': 0.16; 'to:addr:pearwood.info': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.16; 'have:': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'skip:" 20': 0.26; 'message- id:@mail.gmail.com': 0.27; 'sequence': 0.27; 'function': 0.28; 'key,': 0.29; 'url:wikipedia': 0.29; 'url:wiki': 0.30; "d'aprano": 0.33; 'steven': 0.33; 'gets': 0.35; 'received:google.com': 0.35; 'next': 0.35; 'item': 0.35; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:209': 0.38; 'url:en': 0.39; 'some': 0.40; 'your': 0.60; 'here:': 0.63; 'oscar': 0.84; '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:from:date:message-id:subject:to :cc; bh=dMLOlSZ7W9yPEG0x6SKrLlDfCIvvIyN44AxlhfFu/OM=; b=YYI9/d/j+y0MTsudapVX0b4y8oZ5/OYJeCvut3uQ8zNUxk7ZAz81OLxoipODhO04+e KQpUpAGk66FKC+ji8XT/srNL137xT8v8OurGr0T/oFOOwbUSjWdiZ+cK8GS6GXZQfPvH 3QB7pU1MadsefBie1HIA7cG5Oa6I09c6Gm1JmTgx2G2WLkQbl4PvtOkjFP5DmQiTWu3N RWB0td6g9OUDidZ2/5fouxLcfmswQTFTxqgDGP0esKa/k6ZRw9q5Dc+Ht6WNtVjkYz3F 3SStbM/FMbJRVlX/lRlDBioX9VdKuBQ+udx3rTJ0TpnUbg6GUuikObqjl4CnSIfyff9s JScw== 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:from:date :message-id:subject:to:cc; bh=dMLOlSZ7W9yPEG0x6SKrLlDfCIvvIyN44AxlhfFu/OM=; b=bePeC/9fuGXB9PJGZ6pKB8YX0/UVanNiSb9C5jKaPNH58dbVLe1P8LEE2nYnhJHoCu kl75qh9PAV/5cx/cIqUlsgWkTwAEvrh3sOQLjvcLY9KzA5+ZegFy1OMztI/7UCa0i5b0 EtpxVw64fDf9xUarjBBiN2qlBLovwYGpzFI8Ri+mKgqf6BykHuxxI22wXzA/RYlp+5XR aHHyRtn0DwohejarGGxOe11R3wuB8iwV90cuy0pY+OB9ZoWa7aY0nHJg9RucUSgez1U4 XxvzyFLwSNgpkqTdWhQNuJnbhWhhCKhbKf2lLWnlULPVFkKkb1ygVerDW2IkW5d6ccIf OGYQ== X-Gm-Message-State: AOPr4FXlacpFpf7bomaP+NHXHuCw0h1pUSU7w0XgaXa5mY/oNIsTiF61fms9BwxGBszLxHAt63v1WvRGCjEw/w== X-Received: by 10.112.16.36 with SMTP id c4mr5715778lbd.90.1461228804292; Thu, 21 Apr 2016 01:53:24 -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:107444 On 21 April 2016 at 04:07, Steven D'Aprano wrote: > I want to group repeated items in a sequence. For example, I can group > repeated sequences of a single item at a time using groupby: > > > from itertools import groupby > for key, group in groupby("AAAABBCDDEEEFFFF"): > group = list(group) > print(key, "count =", len(group)) > > > outputs: > > A count = 4 > B count = 2 > C count = 1 > D count = 2 > E count = 3 > F count = 4 > > > 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 There are some algorithms (helpfully shown in Python) here: https://en.wikipedia.org/wiki/Cycle_detection Note that those are for a sequence made as x[n+1] = f(x[n]) for some function f. In your case that's just the function that gets the next frame up/down in the call stack. -- Oscar -- Oscar