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)))
相关推荐
酷在前行8 小时前
【R生态】PERMANOVA 进阶实战:距离选择、PERMDISP、两两比较与受限置换(保姆级教程)
开发语言·r语言
cxr8288 小时前
Graphify vs GitNexus vs CodeGraph — 三工具架构深度对比
开发语言·架构·知识图谱
MC皮蛋侠客8 小时前
SQLAlchemy 系列(七):高级建模与高效写入——批量 DML、方言与扩展
数据库·python
Zane19949 小时前
@property 到底是怎么把方法伪装成属性的?一文吃透 property、staticmethod、classmethod
后端·python
qq_316411039 小时前
AI 情感陪伴智能潮玩软硬件一体化开发案例
人工智能·python
废弃的小码农9 小时前
功能测试--Day07--Python编程基础
开发语言·python
zx1154509 小时前
大模型工具调用次数限制
人工智能·python
fengci.10 小时前
Microweber CMS 未授权路径穿越漏洞(CVE-2026-65694)
android·开发语言·前端·学习·php
MC皮蛋侠客10 小时前
SQLAlchemy 系列(八):AsyncIO、并发与 Web 生命周期——让每个并发任务持有自己的 Session
数据库·python
程序员zgh10 小时前
C++ 拷贝赋值运算符 详解
c语言·开发语言·c++