Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31855

Re: get each pair from a string.

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!ecngs!feeder.ecngs.de!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
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; 'output': 0.04; 'string.': 0.04; '21,': 0.07; 'none)': 0.07; 'def': 0.10; ':-)': 0.13; 'input:': 0.16; 'itertools': 0.16; 'oct': 0.16; 'recipe': 0.16; 'wrote:': 0.17; 'header:In-Reply-To:1': 0.25; 'message- id:@mail.gmail.com': 0.27; 'maybe': 0.29; 'received:209.85.215.46': 0.30; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'end': 0.40; 'skip:n 10': 0.63; 'obvious': 0.71; 'subject:get': 0.81; 'to:name:python': 0.84
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 :content-type; bh=Xdj3Xp8vixEs4sjrOoLsmCNwUrFRfM+1MwetFCglWE0=; b=Lae7PEob6kO8pYNqPd18OQEm+MSnK+mBFzYxaLRDdYCqaguLjDFNBL58dCNxLzs3Fa nxnH7ON7lnmuN2h3WA/9MBfCzMPRA3Mnxxcexa8VN6m/HWPbpO8lzKbA6qtkXTHKX+wL SATTAmg4YUe/L691Tzn6wA8Rvg63PZpdNyK3F4B3d1TtlC6Dw2/1fsV4CI/HYJpmSPVM c8LliCYWDn3n5q+H0RlmIQCMJwKoKvRGZHQGTKd0Rmj6i+5EfZcOQ6pgYVtOXP/qAn40 bKuwCSkwEmQEqDTGtm9W/nnghAwmVUfKOm87t/2aO9va1bY+jkRi7mXvAfn0xz48RPnG A9vA==
MIME-Version 1.0
In-Reply-To <CALyJZZURWQxgPTQcQo8kOdHkaOYbB6YE=U=vi6VrX9FoDH1gAQ@mail.gmail.com>
References <CALyJZZURWQxgPTQcQo8kOdHkaOYbB6YE=U=vi6VrX9FoDH1gAQ@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sun, 21 Oct 2012 12:51:51 -0600
Subject Re: get each pair from a string.
To Python <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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2595.1350845543.27098.python-list@python.org> (permalink)
Lines 32
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350845543 news.xs4all.nl 6938 [2001:888:2000:d::a6]:53169
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31855

Show key headers only | View raw


On Sun, Oct 21, 2012 at 12:33 PM, Vincent Davis
<vincent@vincentdavis.net> wrote:
> 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 :-)

Use the "pairwaise" recipe from the itertools docs:

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

> In the end I am going to what to get triples, quads....... also.

Generalizing:

def nwise(iterable, n=2):
    iters = tee(iterable, n)
    for i, it in enumerate(iters):
        for _ in range(i):
            next(it, None)
    return izip(*iters)

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: get each pair from a string. Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-21 12:51 -0600

csiph-web