Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
串联字母数字的自动增量。(Python)_Python_Auto Increment - Fatal编程技术网

串联字母数字的自动增量。(Python)

串联字母数字的自动增量。(Python),python,auto-increment,Python,Auto Increment,我定义了一个生成ID的函数。每次调用该函数时,我都需要增加数字 因此,我使用max()函数查找列表中存储的最后一个最大值。 根据要求,我的ID也应该由整数前面的字符串组成 因此,我将一个字符串与一些存储在列表中的数字连接起来 现在我的max()不起作用,因为在将其连接成字母数字后。请帮助。我尝试拆分ID,但会拆分每个字符 以下是我定义的函数: #!/usr/bin/python from DB import * def Sid(): idlist = []

我定义了一个生成ID的函数。每次调用该函数时,我都需要增加数字

因此,我使用
max()
函数查找列表中存储的最后一个最大值。 根据要求,我的ID也应该由整数前面的字符串组成

因此,我将一个字符串与一些存储在列表中的数字连接起来

现在我的
max()
不起作用,因为在将其连接成字母数字后。请帮助。我尝试拆分ID,但会拆分每个字符

以下是我定义的函数:

#!/usr/bin/python

from DB import *

def Sid():
        idlist = []

        for key in Accounts:
                if Accounts[key]['Acctype'] == 'Savings':
                        idlist.append(Accounts[key]['Accno'])

        if len(idlist) == 0:
                return 1001

        else:
                abc = max(idlist)
                return abc + 1
编辑1: 下面是我调用函数的方式:

  accno = Sid()
  AppendRecord(Accno="SA"+str(accno))
idlist.append(number_suffix(Accounts[key]['Accno']))

您可以从字符串中去掉数字后缀,以获得要递增的数字:

import re

def number_suffix(s):
    """Return the number from the end of the string. """
    match = re.search(r"\d+$", s)
    if match:
        num = int(match.group(0))
    else:
        num = 0
    return num

print number_suffix("AS1001")    # 1001
print number_suffix("AS1")       # 1
print number_suffix("AS")        # 0
然后更改您的功能:

  accno = Sid()
  AppendRecord(Accno="SA"+str(accno))
idlist.append(number_suffix(Accounts[key]['Accno']))