Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Vlastimil Brom Newsgroups: comp.lang.python Subject: Re: Detecting repeated subsequences of identical items Date: Thu, 21 Apr 2016 08:54:04 +0200 Lines: 105 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 EEQRCiR0FtllntokUuiZuQnx2umtELAwwZ/Ujdm/xang== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'repeated': 0.07; 'output': 0.13; "''),": 0.16; "('b',": 0.16; "('c',": 0.16; 'grouped': 0.16; 'itertools': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sequence.': 0.16; 'subject:Detecting': 0.16; 'have:': 0.18; 'latter': 0.22; 'seems': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'skip:" 20': 0.26; 'message- id:@mail.gmail.com': 0.27; 'grouping': 0.29; 'key,': 0.29; 'url:mailman': 0.30; 'skip:[ 10': 0.31; 'problem': 0.33; 'url:python': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'url:listinfo': 0.34; 'this?': 0.34; 'received:google.com': 0.35; 'item': 0.35; 'url:org': 0.36; 'received:209.85': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'missing': 0.37; 'received:209': 0.38; 'hi,': 0.38; 'does': 0.39; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'more': 0.63; 'or:': 0.84; 'to:name:python': 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:date:message-id:subject:from:to; bh=AJVzcJXV7vlX+/Ylp0sUZRunjl8n0JqTy0/+OUjWK3k=; b=vZ5PmwVn3ipx8MkNRnnlt63wl18NirFZxEmAbEZkHLRcM2z7vGxco6W8AZaOjxR7/z 7iRBr5EYlk3bExtqAe4iOiCPBo+jg3vRqO6YZSMc81m5XR/IS8Xnh5Hp1XC2+5l2I+Pt HHR9+Hu+9GXIAkzyBsElsj7cZ5MXIL3m+W98F9Ju/2bujmRKzqVmvfAh47geuOL8PVZB mMY9xFKoPXQ4swbsTcxc0OZji/8zwn51LpEESVmUfy4HFNDh9USbn6GF8iTSK8ZkOcrk yOl6HYtqv70MxqeY95zx3cboNEfXLaf9GV2917MOY4kt65ebIMp8MfEEetNyAH6+Ej4K II4A== 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:to; bh=AJVzcJXV7vlX+/Ylp0sUZRunjl8n0JqTy0/+OUjWK3k=; b=bMJd2ykjWqVqmIIfDuW4MiE6yrKO2hzjt1ZLRmN1PFYPS2rJz/C0qz227Gw2Rhz/Bc E7Ar1zNhTj4SwttUMw25WWHfNdoBGArktrxBhbCDkIuwBjerkdndZTaGOcZp5yhq5IU+ QjG5yisRT7A4CN6/rBbg+9+8H7BkVOdpWPzPj2W35AzLlTJ9hHKKSIh3Brx+BmXhHtiX jz4EKgUB9RzciiH3HkxBiLk/vJ9ju3qrt6EtysFMeeVQUIXVIGq2BMbvL9Sy68cin/Dk VHXWG3NbAAqlC/IQV3YJR/Mc0cNeJjMlWPFALitMgH+nMnrVc5WKxU1ysQXA5PVzgd+J 3WzQ== X-Gm-Message-State: AOPr4FXpLBim6K8Kh5Ba42I+ccQEai5DtR9Rdao/9wD5wkIDUTksVgBEj6Jwe4QdAA3QPfMAIBZq6i9FP99KQw== X-Received: by 10.112.2.104 with SMTP id 8mr5493509lbt.50.1461221644754; Wed, 20 Apr 2016 23:54:04 -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:107439 2016-04-21 5:07 GMT+02:00 Steven D'Aprano : > 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 > > giving counts: > > (ABC) count = 2 > (ABCDE) count = 2 > F count = 1 > (ABC) count = 3 > B repeats 1 time > > > or: > > ABC ABC ABC D E A B C D E F ABC ABC ABC B > > giving counts: > > (ABC) count = 3 > D count = 1 > E count = 1 > A count = 1 > B count = 1 > C count = 1 > D count = 1 > E count = 1 > F count = 1 > (ABC) count = 3 > B count = 1 > > > > How can I do this? Does this problem have a standard name and/or solution? > > > > > -- > Steven > > -- > https://mail.python.org/mailman/listinfo/python-list Hi, if I am not missing something, the latter form of grouping might be achieved with the following regex: t="ABCABCABCDEABCDEFABCABCABCB" grouped = re.findall(r"((?:(\w+?)\2+)|\w+?)", t) print(grouped) for grp, subseq in grouped: if subseq: print(subseq, grp.count(subseq)) else: print(grp, "1") the printed output is: [('ABCABCABC', 'ABC'), ('D', ''), ('E', ''), ('A', ''), ('B', ''), ('C', ''), ('D', ''), ('E', ''), ('F', ''), ('ABCABCABC', 'ABC'), ('B', '')] ABC 3 D 1 E 1 A 1 B 1 C 1 D 1 E 1 F 1 ABC 3 B 1 The former one seems to be more tricky... hth, vbr