如何更改此列表中的项目?我想改变
[A1 (76113),A2 (7614),...,A11 (73887)]
到
[A01, A02,..., A11]
谢谢
答案 0 :(得分:0)
这里有一个简单的方法,就是删除数字,然后将“ A1”改成A01”。它假定每个项目都是一个字符串,如果不正确,请告诉我。
def transItem(item):
parts = item.split()
aLet = parts[0][0]
aNum = parts[0][1:]
if len(aNum) <=1:
aNum = "0"*(2-len(aNum)) + aNum
return aLet+aNum
oldList = ["A1 (76113)","A2 (7614)","A11 (73887)"]
newList = [transItem(itm) for itm in oldList]