0

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
  • 6,348
  • 7
  • 46
  • 58
BurmesePapi
  • 69
  • 1
  • 2
  • 8

5 Answers5

3

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
  • 28,402
  • 9
  • 97
  • 94
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']
Harry_pb
  • 4,892
  • 1
  • 30
  • 44
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,280
  • 2
  • 17
  • 34
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
  • 9,140
  • 4
  • 35
  • 62
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)