1

Is there any algorithm to get number of repeating overlap substrings in an string? This question is similar to Splitting a Python list into a list of overlapping chunks, but in my case changed elements are first and last elements of each substring! for example when:

string = "HelloWorld"
k = 4

then:

subString = ["Hell","ello","lloW","loWo","oWor","Worl","orld"]

i tried this:

i = 0
while len(string[i+4:])>=4:
    i+=1
print i
Monako
  • 51
  • 4
  • 4
    `substr = [ string[i:i+2] for i in range(len(string)-1)]` ? – Patrick Artner Jun 17 '18 at 12:51
  • 2
    Show what have you tried. – mentallurg Jun 17 '18 at 12:51
  • 2
    and unless you throw in a `lower()` it wont change the capital `H` to lower `h` ... – Patrick Artner Jun 17 '18 at 12:52
  • 1
    @PatrickArtner you should post that answer, I don't think there is a dupe for that one in particular as it's prettry general with the k-length substring, moreover: overlapping. – Olivier Melançon Jun 17 '18 at 12:55
  • 1
    @Patrick Artner so true you are. – vahdet Jun 17 '18 at 12:56
  • 1
    @PatrickArtner Oh you are right, I didn't look for answers with lists – Olivier Melançon Jun 17 '18 at 13:03
  • @PatrickArtner Threre is not dupe, in my case, all elements of the each previous sub-list except first element include as elements in the next sub-list. – Monako Jun 17 '18 at 13:35
  • @Monako See my answer then – Olivier Melançon Jun 17 '18 at 13:43
  • @OlivierMelançon Oh, Yes, you are right. – Monako Jun 17 '18 at 13:49
  • It is a dupe - if you need more then 2 characters (which was the OP before you edited) then you simply have to change the amount of characters you take in the sublist. I do not really think that we need 10 answers for sublists of 2,3,4,5,6,7,8,9,10,11 things. If you wanted 4 chracters you would use `substr = [ string[i:i+4] for i in range(len(string)-3)]` - the -3 making sure you only get length 4 sublists. @OlivierMelançon kindly wrote an answer, you could accept his as it does what you wanted. For other answers look into the answers of the dupe. Being a dupe does not mean your OP is bad. – Patrick Artner Jun 17 '18 at 14:19
  • @PatrickArtner Thank you so much – Monako Jun 17 '18 at 14:28

1 Answers1

2

You can return all k-length slices of the strings with a list-comprehension.

def substrings(s, k=2):
    return [s[i:i+k] for i in range(len(s) - k + 1)]

s = "Hello"

print(substrings(s, k=2)) # ['He', 'el', 'll', 'lo']
print(substrings(s, k=3)) # ['Hel', 'ell', 'llo']
Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73