python --创建固定字符串长度,先进先出

python 复制代码
a = '123'

def concatenate_within_limit(b, new_string):
    # 计算新字符串与a的长度之和
    a = b
    total_length = len(a) + len(new_string)

    # 如果长度超过1024,从前面删除足够的字符
    if total_length > 5:
        diff = total_length - 5
        a = a[diff:] + new_string  # 删除前diff个字符,并拼接新字符串
    else:
        a += new_string  # 长度未超过1024,直接拼接新字符串

    # 返回处理后的a
    return a

for i in range(4,10):
    print(concatenate_within_limit(a, str(i)))
相关推荐
jimy15 分钟前
polymorph.cpp里面的“析构函数、智能指针”问题
开发语言·c++
m0_380743878 分钟前
PHP 8.3 缓存清除不生效怎么排查
开发语言·php
happylifetree16 分钟前
Python14:核心语法-数据存储与运算-字符串定义
python
hhb_61818 分钟前
AIRAGDebug:一键定位RAG链路异常
人工智能·python·算法
朝朝辞暮i36 分钟前
C++ 第 6 课:switch —— 多状态选择
开发语言·c++·算法
小静AI工程实验室40 分钟前
asyncio.Queue 有界队列实战:背压、task_done、join 与异常收尾
python
小凡geo43 分钟前
本地商家 GEO:用脚本一键生成 FAQPage 结构化数据,让 AI 切片更稳
开发语言·人工智能·python·microsoft·搜索引擎·ai
Zhou1411361 小时前
SpringBoot_03_Web开发
前端·spring boot·python
weixin199701080161 小时前
《1688物流API接口边界:logistics.trace.get 与 freight.template.list 的隐藏约束》(附Python源码)
windows·python·list
朝朝辞暮i1 小时前
C++ 第 4 课:if / else —— 让程序自己做决定
开发语言·c++