V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
think2011
V2EX  ›  问与答

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

  •  
  •   think2011 · Jun 14, 2022 · 673 views
    This topic created in 1414 days ago, the information mentioned may be changed or developed.
    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

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

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4020 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 00:52 · PVG 08:52 · LAX 17:52 · JFK 20:52
    ♥ Do have faith in what you're doing.