Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'url:pypi': 0.03; 'output': 0.04; 'string.': 0.04; 'newline': 0.07; ':-)': 0.13; 'library': 0.15; '(assuming': 0.16; 'input:': 0.16; '>>>': 0.18; 'input': 0.18; 'appropriate': 0.20; 'import': 0.21; 'header:In-Reply-To:1': 0.25; 'appear': 0.26; 'message-id:@mail.gmail.com': 0.27; 'url:mailman': 0.29; 'probably': 0.29; 'maybe': 0.29; 'e.g.': 0.30; 'url:python': 0.32; 'could': 0.32; 'url:listinfo': 0.32; 'text,': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'thanks': 0.34; 'sequence': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'end': 0.40; 'url:mail': 0.40; 'obvious': 0.71; 'subject:get': 0.81; 'canonical': 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 :content-type; bh=GE+lx3lp27Dh7mgc8cwCy/QYEfURWzJrhxxOCHOXg6g=; b=elXxmxuINjWCwYcSxa87FkDvpphsJFp2o5V7nRy4wgdOpxpskLLYQENBnrW45rl6v2 jROcN4mzysbYkFSNeXY1tVfdW3Pv9yHl6tmKPVbMnwz+gCd0sK+SRc8TffuQ5LHnRulX nSXohsc1y8iCyn/eYlvwUAjPKv1cLJT8ArRuxwC5HT4iiWNo5ELkuEl4e9I2B83TlZwB 9uCQGgncX9n54uq2fr1oRKGz/5ZDaOIsjMMxfwYj3tDNevH9p3efhWao7+IpGr2Go35D bVK3iMAUyRJtoGramXKsR16Dk719kO6qGRitMEAO+IUL0ADiyKbTGc6NTqpTCI6or296 uGeQ== MIME-Version: 1.0 In-Reply-To: References: Date: Sun, 21 Oct 2012 23:48:43 +0200 Subject: Re: get each pair from a string. From: Vlastimil Brom To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1350856133 news.xs4all.nl 6946 [2001:888:2000:d::a6]:58646 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31864 2012/10/21 Vincent Davis : > I am looking for a good way to get every pair from a string. For example, > input: > x = 'apple' > output > 'ap' > 'pp' > 'pl' > 'le' > > I am not seeing a obvious way to do this without multiple for loops, but > maybe there is not :-) > In the end I am going to what to get triples, quads....... also. > > Thanks > Vincent > > > -- > http://mail.python.org/mailman/listinfo/python-list > Hi, just another - probably less canonical - approach using the new regex library could be (assuming the input sequence is always a string): >>> import regex # http://pypi.python.org/pypi/regex >>> regex.findall("..", "abcdefghijklm", overlapped=True) ['ab', 'bc', 'cd', 'de', 'ef', 'fg', 'gh', 'hi', 'ij', 'jk', 'kl', 'lm'] >>> regex.findall("...", "abcdefghijklm", overlapped=True) ['abc', 'bcd', 'cde', 'def', 'efg', 'fgh', 'ghi', 'hij', 'ijk', 'jkl', 'klm'] >>> If the newline \n could appear in the text, an appropriate pattern would be e.g. "(?s)..." regards, vbr