1

I'm trying to get a list of substrings of a given length out of a string.

For example, if I have a string

word = "PYTHON"

and the specified substring length is 4, how can I obtain the following list?

['PYTH', 'YTHO', 'THON']

Here is my attempt:

size = 4
win = [] 
word = "PYTHON"

i = iter(word)

for x in range(0,size):
    win.append(next(i))
print(win)

for e in i:
    win = win[1:] + [e]            
    print(win)
2

5 Answers 5

4

It seems you want a sliding window. Consider this more_itertools third-party tool:

import more_itertools as mit


word = "PYTHON"
["".join(w) for w in mit.windowed(word, 4)]
# ['PYTH', 'YTHO', 'THON']
Sign up to request clarification or add additional context in comments.

Comments

1

You can just use join operation in your code to print needed string. Example:-

size = 4
win = [] 
word = "PYTHON"
final_list = []

i = iter(word)

for x in range(0,size):
    win.append(next(i))
final_list.append(''.join(win))

for e in i:
    win = win[1:] + [e]            
    final_list.append(''.join(win))

print (final_list)


>>['PYTH', 'YTHO', 'THON']

Comments

0

You can do this as

size = 4
win = [] 
word = "PYTHON"

for i in range(0, len(word)-size + 1):
    win.append(word[i:i+size])
print(win)

Or with a list comprehension as

size = 4
word = "PYTHON"

win = [word[i:i+size] for i in range(0, len(word)-size + 1)]
print(win)

Comments

0

You can try this approach:

word = "PYTHON"

print([word[i:i+4] for i in range(0,len(word),1) if len(word[i:i+4])==4])

output:

['PYTH', 'YTHO', 'THON']

or you can also try recursive approach:

word = "PYTHON"

def recursive_approach(data,window_size,final_result=[]):
    if len(data[:4])==window_size:
        final_result.append(data[:4])
        return recursive_approach(data[1:],4)
    return final_result


print(recursive_approach(word,4))

output:

['PYTH', 'YTHO', 'THON']

Comments

0

Try without [] and replace them with "".

    size = 4
    win = [] 
    word = "PYTHON"

    i = iter(word)

    for x in range(0,size):
        win.append(next(i))
    print(win)

    for e in i:
       win = win[1:] + "e"            
       print(win)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.