let's say I have a string
s = 'Hello world, I am Foo. I like dogs'
I want to parse thru this string and extract words in groups of x
. Let's say x = 3
, my output should be:
out = ['Hello world, I', 'world, I am', 'I am Foo.', 'am Foo. I', 'Foo. I like', 'I like dogs']
Is there a way to do this? I tried this code but it's very bad.
sss = s.split()
x = 3
for i, word in enumerate(sss):
for j in range(x):
extract = sss[j+i:x+j]
print(extract)
out: ['Hello', 'world,', 'I']
['world,', 'I', 'am']
['I', 'am', 'Foo.']
['world,', 'I']
['I', 'am']
['am', 'Foo.']
['I']
['am']
['Foo.']
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]