如何删除字符串中的前导和尾随零?蟒蛇
我有几个像这样的字母数字字符串 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] …