このような英数字の文字列がいくつかあります
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)
s = '0'
か?