Python join more_itertools.windowed results

1k Views Asked by At

I have a following problem: I am trying to create so-called "digrams", like this:

If I have a word foobar, I want to get a list or a generator like: ["fo", "oo", "ob", "ba", "ar"]. The perfect function to that is more_itertools.windowed. The problem is, it returns tuples, like this:

In [1]: from more_itertools import windowed

In [2]: for i in windowed("foobar", 2):
   ...:     print(i)
   ...:
('f', 'o')
('o', 'o')
('o', 'b')
('b', 'a')
('a', 'r')

Of course I know I can .join() them, so I would have:

In [3]: for i in windowed("foobar", 2):
   ...:     print(''.join(i))
   ...:
   ...:
fo
oo
ob
ba
ar

I was just wondering if there's a function somewhere in itertools or more_itertools that I don't see that does exactly that. Or is there a more "pythonic" way of doing this by hand?

2

There are 2 best solutions below

0
On BEST ANSWER

You can write your own version of widowed using slicing.

def str_widowed(s, n):
    for i in range(len(s) - n + 1):
        yield s[i:i+n]

This ensure the yielded type is the same as the input, but no longer accepts non-indexed iterables.

0
On

more_itertools.windowed() is pythonic. Consider the pairwise() itertools recipe that also yields tuples:

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

You can easily replace windowed() with pairwise() and get common results - a generic solution.


Alternatively, you can slice strings by emulating the pairwise principle of zipping duplicate but offset strings:

Code

s = "foobar"

[a + b for a, b in zip(s, s[1:])]
# ['fo', 'oo', 'ob', 'ba', 'ar']