我想用python编写一个代码,它将用“a”替换每个小写字母,每个大写字母用“A”替代,每个数字替换为0.我编写代码但它造成的x上的错误不在列表中,代码如下如何替换python中的列表中的字符
tokens = ["apple","banana","Orange", "pineApple", "10nuts"]
for token in tokens:
for ch in token:
if ch.islower():
loc = tokens.index(ch)
tokens.remove(ch)
tokens.insert(loc,'a');
elif ch.isupper():
loc = tokens.index(ch)
tokens.remove(ch)
tokens.insert(loc,'A');
elif ch.isdigit():
loc = tokens.index(ch)
tokens.remove(ch)
tokens.insert(loc,'0');
for t in tokens:
print t
这可能是[regex]的一个很好的用法(https://docs.python.org/2/library/re.html) – pzp 2015-04-01 03:00:38
你可以帮我在那个reepect – 2015-04-01 03:05:03
我刚回答说明如何去做。 – pzp 2015-04-01 03:16:08