Auto increment of Concatenated alpha-numeric. (Python)
I have defined a function that generates ID. I need to increment the numbers every time I call the function.
So I used max()
function to find the last largest value stored in the list.
According to the requirements my ID should also consist of a string in front of the integers.
So I have concatenated a string with some numbers which have been stored in a list.
Now my max()
does not work,because after concatenating its converted into a alpha-numeric.Please help.I tried splitting the ID but that splits each character.
Following is the function I defined:
#!/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
开发者_StackOverflow社区 else:
abc = max(idlist)
return abc + 1
EDIT 1: Here is how I have called the function:
accno = Sid()
AppendRecord(Accno="SA"+str(accno))
You can strip off the numeric suffix from the string to get a number to increment:
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
then change your function:
idlist.append(number_suffix(Accounts[key]['Accno']))
精彩评论