V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
think2011
V2EX  ›  问与答

请问这段生成不重复也不长的订单号的 Python 代码原理是什么?

  •  
  •   think2011 · 2022 年 6 月 14 日 · 629 次点击
    这是一个创建于 1391 天前的主题,其中的信息可能已经有所发展或是发生改变。
    tmp_num = self.id + 1000000
    nums = map(int, '%07d' % tmp_num)
    factors = map(int, '123456')
    result = sum(
        map(lambda args: args[0] * args[1], zip(factors, nums))) % 11
    if result == 0:
        salt = 5
    elif result == 1:
        salt = 0
    else:
        salt = 11 - result
    
    number = "DD{:07d}{}".format(tmp_num, salt)
    

    例如会生成 DD10013782

    生成出来的订单号不长,也不会重复,好奇什么样的逻辑?

    目前尚无回复
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   2534 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 11:51 · PVG 19:51 · LAX 04:51 · JFK 07:51
    ♥ Do have faith in what you're doing.