Django/Python:如何使以下数字递增(不在数据库中)

发布于 2024-08-16 07:05:51 字数 252 浏览 4 评论 0原文

我想创建一个像这样的数字:

000000000001

保存到数据库中。我显然不能在数据库中以这种方式递增(我不认为),所以我正在寻找最有效的方法来从数据库中提取前一个数字并将其递增 1 以创建下一条记录:

000000000002

等等...

如果我手动存储第一个数字,我可以进行某种手动输入以使其保留零的数量吗?我什至不知道从哪里开始。

I would like to create a number like:

000000000001

to save to the database. I obviously cannot increment in this fashion (I don't think) in a database, so I'm looking for the most efficient method for pulling the previous number from the database and incrementing it by 1 to create the next record:

000000000002

and so on...

If I store the first number manually, can I do some sort of manual typing to make it hold its number of zeros? I don't even know where to start.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

香橙ぽ 2024-08-23 07:05:51

所有前导零只是格式化。

>>> "%012d" % ( 1, )
'000000000001'
>>> "%012d" % ( 2, )
'000000000002'

使用普通整数并将其格式化为包含大量前导零。

All the leading zeroes are just formatting.

>>> "%012d" % ( 1, )
'000000000001'
>>> "%012d" % ( 2, )
'000000000002'

Use an ordinary integer and format it to have lots of leading zeroes.

尤怨 2024-08-23 07:05:51

实际上有一种非常棘手的方法可以使用 itertools 库和生成器函数来做到这一点。

from itertools import product, imap

def stringdigit(num_digits=10, start = None):
    """A generator function which returns string versions of a large iterated number with
    leading zeros. start allows you to define a place to begin the iteration"""
    treatfun = lambda x: ''.join(x)
    for n in imap(treatfun, product('0123456789', repeat = num_digits)):
        if start == None or n > start:
            yield n

这将创建一个迭代器,它将返回您需要的“零填充字符串形式”。它使用 Product 函数来工作,该函数以“排序顺序”迭代地从可迭代返回重复组合。 num_digits 参数指定您想要返回的总位数。 start 指定迭代开始的位置(假设您想从 1111111 开始)。

product 随 python 2.6 版本一起提供。如果您出于某种原因使用之前的某些内容,请使用它作为产品定义。取自此处的文档。

def product(*args, **kwds):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = map(tuple, args) * kwds.get('repeat', 1)
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

您可以在 for 循环中使用此函数作为迭代器:

for num in stringdigit(num_digits = 7):
    #do stuff with num

希望有所帮助。
-将要

There's actually a super tricky way to do this using the itertools library and a generator function.

from itertools import product, imap

def stringdigit(num_digits=10, start = None):
    """A generator function which returns string versions of a large iterated number with
    leading zeros. start allows you to define a place to begin the iteration"""
    treatfun = lambda x: ''.join(x)
    for n in imap(treatfun, product('0123456789', repeat = num_digits)):
        if start == None or n > start:
            yield n

This creates an iterator which will return the "zero-padded string form" that you need. It works using the product function which iteratively returns repeated combinations from an iterable in "sorted order". The num_digits argument specifies how many total digits you would like returned. start specifies a place to begin the iteration from (say if you wanted to start from 1111111).

product comes with the python 2.6 release. If your using something before that for some reason then use this as the product definition. Taken from the docs here.

def product(*args, **kwds):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = map(tuple, args) * kwds.get('repeat', 1)
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

You can use this function in in a for-loop as an interator:

for num in stringdigit(num_digits = 7):
    #do stuff with num

Hope that helps.
-Will

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文