You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python如何基于字符数字混合起始值生成递增用户ID序列

字符数字混合用户ID序列批量生成Python实现

核心思路为拆分起始ID的固定前缀与末尾连续数字段,保留数字段的前导零长度后,按指定数量递增拼接即可,实现代码如下:

import re

def generate_id_series(start_id: str, count: int) -> list:
    # 匹配末尾连续数字段,拆分前缀和数字部分
    match_result = re.search(r'(\d+)$', start_id)
    if not match_result:
        raise ValueError("起始ID末尾必须包含连续数字")
    num_suffix_str = match_result.group(1)
    prefix = start_id[:match_result.start()]
    # 记录数字部分长度,用于补全前导零
    num_length = len(num_suffix_str)
    start_num = int(num_suffix_str)

    result = []
    for i in range(count):
        current_num = start_num + i
        current_suffix = str(current_num).zfill(num_length)
        result.append(f"{prefix}{current_suffix}")
    return result

# 示例测试
if __name__ == "__main__":
    start_id = "A10B00001"
    generate_count = 100
    id_series = generate_id_series(start_id, generate_count)
    for _id in id_series:
        print(_id)

代码说明

  • 用正则表达式(\d+)$匹配ID末尾的连续数字,不会误拆分前缀中穿插的数字
  • 调用zfill方法自动补全前导零,严格保留原起始ID的数字段长度,超出原长度时按实际位数输出
  • 增加了输入合法性校验,避免传入不符合规则的起始ID

运行示例代码即可输出需求中从A10B00001到A10B00100的100条ID序列。

内容的提问来源于stack exchange,提问作者Gokul Prasanth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 21:00:02