Let's say I have a list of words, something like this:
['The', 'Quick', 'Brown', 'Fox', 'Jumps', 'Over', 'The', 'Lazy', 'Dog']
I'd like to generate a list of lists, with each array containing 3 of the words, but with a possible triplet for each one. So it should look something like this:
['The', 'Quick', 'Brown']
['Quick', 'Brown', 'Fox']
['Brown', 'Fox', 'Jumps']
and so on. What would be the best way to get this result?
>>> words
['The', 'Quick', 'Brown', 'Fox', 'Jumps', 'Over', 'The', 'Lazy', 'Dog']
>>> [words[i:i+3] for i in range(len(words) - 2)]
[['The', 'Quick', 'Brown'], ['Quick', 'Brown', 'Fox'], ['Brown', 'Fox', 'Jumps'], ['Fox', 'Jumps', 'Over'], ['Jumps', 'Over', 'The'], ['Over', 'The', 'Lazy'], ['The', 'Lazy', 'Dog']]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With