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

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

  •  
  •   think2011 · 2022-06-14 17:52:34 +08:00 · 361 次点击
    这是一个创建于 698 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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   ·   实用小工具   ·   2566 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 15:57 · PVG 23:57 · LAX 08:57 · JFK 11:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.