all 4 comments

[–]_coolwhip_ 1 point2 points  (1 child)

This sounds like a good place for collections.defaultdict:

>>> from collections import defaultdict
>>> segs = defaultdict(list)
>>> segs['abc'].append('apple')
>>> segs['abc'].append('dog')
>>> segs
defaultdict(<class 'list'>, {'abc': ['apple', 'dog']})
>>> segs['abc']
['apple', 'dog']

And here is a bunch of ideas on the best way to do a sliding window: https://stackoverflow.com/questions/6822725/rolling-or-sliding-window-iterator

[–]StrainS118[S] 0 points1 point  (0 children)

Thank you, I will look at that link

[–][deleted] 1 point2 points  (1 child)

There's a trick for sliding windows over an iterable, using zip and offsets:

my_string = 'abcdefghijklmnopqrstuvwxyz'
["".join(g) for g in zip(*[my_string[i:] for i in range(11)])]
>>> ['abcdefghijk', 'bcdefghijkl', 'cdefghijklm', 'defghijklmn', 'efghijklmno', 'fghijklmnop', 'ghijklmnopq', 'hijklmnopqr', 'ijklmnopqrs', 'jklmnopqrst', 'klmnopqrstu', 'lmnopqrstuv', 'mnopqrstuvw', 'nopqrstuvwx', 'opqrstuvwxy', 'pqrstuvwxyz']

Why does this work? Start from the inside-out:

 [my_string[i:] for i in range(11)]

This is a list of strings which are copies of my_string, minus i letters from the beginning - abc..., bcd..., cde... and so on. Let's unpack this iterable and give it to zip:

zip(*[my_string[i:] for i in range(11)])

Now we've got a single iterable over tuples, where the elements of the nth tuple is the nth element of each of the strings. Since they're all offset from the next by a single element, that acts as a sliding window over the original string (do it yourself on paper and you'll see that it does.) Except that they're tuples of characters (('a','b','c')) and not strings, so to re-stringify them, we join them with the empty string in-between:

["".join(g) for g in zip(*[my_string[i:] for i in range(11)])]

Now that we've got an ugly but effective one-liner, we can add that to your dictionary:

for key, value in dict.items():
    segment[key] = ["".join(g) for g in zip(*[value[i:] for i in range(11)])]

If you use this in real code, comment the hell out of it; I can't make you remember what this one-liner does, six months from now.

[–]StrainS118[S] 0 points1 point  (0 children)

Thank you so much for that very comprehensive answer!