3

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?

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
dabljues
  • 1,663
  • 3
  • 14
  • 30
  • 1
    `windowed` seems pretty pythonic to me. – Pedro Lobito Apr 08 '19 at 00:41
  • i'm not sure you need `windowed` here, a `zip(s, s[1:])` works just as well. One way to avoid the tuples is `[s[i:i+2] for i in range(len(s) - 1)]` – iruvar Apr 08 '19 at 01:48
  • These are also called the q-grams of length 2. I am a fan of q-gram filtration for edit distance similarity search. – Dan D. Apr 08 '19 at 05:31

2 Answers2

2

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.

Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
1

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']
pylang
  • 40,867
  • 14
  • 129
  • 121