社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Karls  »  全部回复
回复总数  1
7 年前
回复了 Karls 创建的主题 » 在python中如何根据索引和长度来无限字符串?

不绕圈子怎么样?

我们有三部分: 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,可以考虑使用此解决方案。