我有几个像这样的字母数字字符串
listOfNum = ['000231512-n','1209123100000-n00000','alphanumeric0000', '000alphanumeric']
删除尾随零的所需输出将是:
listOfNum = ['000231512-n','1209123100000-n','alphanumeric', '000alphanumeric']
前导尾随零的所需输出将是:
listOfNum = ['231512-n','1209123100000-n00000','alphanumeric0000', 'alphanumeric']
删除前导零和尾随零的期望输出将是:
listOfNum = ['231512-n','1209123100000-n', 'alphanumeric', 'alphanumeric']
现在我一直在做以下方式,如果有,请提出更好的方法:
listOfNum = ['000231512-n','1209123100000-n00000','alphanumeric0000', \
'000alphanumeric']
trailingremoved = []
leadingremoved = []
bothremoved = []
# Remove trailing
for i in listOfNum:
while i[-1] == "0":
i = i[:-1]
trailingremoved.append(i)
# Remove leading
for i in listOfNum:
while i[0] == "0":
i = i[1:]
leadingremoved.append(i)
# Remove both
for i in listOfNum:
while i[0] == "0":
i = i[1:]
while i[-1] == "0":
i = i[:-1]
bothremoved.append(i)