Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #31864 > unrolled thread
| Started by | Vlastimil Brom <vlastimil.brom@gmail.com> |
|---|---|
| First post | 2012-10-21 23:48 +0200 |
| Last post | 2012-10-21 23:48 +0200 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: get each pair from a string. Vlastimil Brom <vlastimil.brom@gmail.com> - 2012-10-21 23:48 +0200
| From | Vlastimil Brom <vlastimil.brom@gmail.com> |
|---|---|
| Date | 2012-10-21 23:48 +0200 |
| Subject | Re: get each pair from a string. |
| Message-ID | <mailman.2607.1350856133.27098.python-list@python.org> |
2012/10/21 Vincent Davis <vincent@vincentdavis.net>:
> 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
Back to top | Article view | comp.lang.python
csiph-web