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)
Georgy
  • 12,464
  • 7
  • 65
  • 73
BurmesePapi
  • 79
  • 1
  • 2
  • 8

5 Answers5

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']
pylang
  • 40,867
  • 14
  • 129
  • 121
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']
Hari_pb
  • 7,088
  • 3
  • 45
  • 53
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)
JahKnows
  • 2,618
  • 3
  • 22
  • 37
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']
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88
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)