不绕圈子怎么样?
我们有三部分:
Begin of string
,
Middle (repetition of word "book")
和
End
def create_sequence(string, index, length):
word_length=len(string)
string_repetition=((length+index)//word_length+(-index)//word_length) #How many full strings we have
string_end=(length+index)%word_length #How manny letters are left after last full word
inf_string_start= string[length-string_repetition*word_length:]
inf_string_mid=string_repetition*string#MID
inf_string_end=string[:string_end] #END
inf_string=inf_string_start+inf_string_mid+inf_string_end #concatenation
return inf_string
然而
itertools
使用起来可能更友好。但如果不想使用外部libs,可以考虑使用此解决方案。