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)))
相关推荐
墨染点香4 分钟前
第七章 Pytorch构建模型详解【构建CIFAR10模型结构】
人工智能·pytorch·python
枫叶丹424 分钟前
【Qt开发】信号与槽(二)-> 信号和槽的使用
开发语言·qt
阿什么名字不会重复呢27 分钟前
在线工具+网页平台来学习和操作Python与Excel相关技能
python·数据分析
Vertira1 小时前
python 阿里云 安装 dashscope的简介、安装
开发语言·python
gc_22992 小时前
学习Python中Selenium模块的基本用法(1:简介)
python·selenium
先做个垃圾出来………3 小时前
2116. 判断一个括号字符串是否有效
python
兮℡檬,3 小时前
房价预测|Pytorch
人工智能·pytorch·python
hqxstudying3 小时前
Java异常处理
java·开发语言·安全·异常
wjs20246 小时前
状态模式(State Pattern)
开发语言
我命由我123456 小时前
Kotlin 数据容器 - List(List 概述、创建 List、List 核心特性、List 元素访问、List 遍历)
java·开发语言·jvm·windows·java-ee·kotlin·list