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)))
相关推荐
SomeB1oody10 小时前
【RustyML入门】3.7. 循环层
开发语言·后端·机器学习·rust·教程
点云-激光雷达-Slam-三维牙齿12 小时前
速度起飞 笔记本电脑6G显卡llama运行Qwen3.6 35BA3B MTP 大模型
人工智能·python·电脑·llama
言乐613 小时前
Python游戏水平测试辅助系统
开发语言·python·游戏·django·pygame
从零开始学习人工智能19 小时前
【踩坑实录】WSL2 解决 onnxruntime\-gpu ImportError: libcudart\.so\.13 无 CUDA13 运行库问题
python
WWJA王文举20 小时前
I²C通信完整流程详解:START、地址、ACK、数据、Repeated START和STOP一次讲透
c语言·开发语言
卷无止境21 小时前
在 awesome-fastapi 里,哪些库值得一看?
后端·python
zhanghaha131421 小时前
Python进阶教程:6_JSON 数据解析 —— 新手完全指南
开发语言·python·json
Python私教21 小时前
API 输出模型怎么设计:从 model_dump() 到显式展示层
python·fastapi
Python私教1 天前
本地 AI 工具服务该绑定 127.0.0.1 还是 0.0.0.0?
python·fastapi
卷无止境1 天前
FastAPI 的Admin面板生态
后端·python