Python如何替换list中的字符

2024-12-16 17:25:58
推荐回答(5个)
回答1:

利用re库进行正则表达替换,python3.4中

import re

a = ["123" , "456" , "789"]
d = {"B":"2","E":"5","H":"8"}
b = ",".join(a)
print (b)

for i in d.keys():
    b = re.sub(d[i],i,b)
print (b.split(","))

回答2:

import re

a = ["!-*%" , ".#7&33&" , "#*#71%"]
d = {"A":"#","N":"%","M":"\*"}
b = ",".join(a)
print b

for i in d.keys():
b = re.sub(d[i],i,b)
print b.split(",")

回答3:

[str(j) for j in list if j]使用列表解析处理下这样的问题

回答4:

假定list为 x

x = [c.replace('#','A') for c in x]

回答5:

For s in list: for c in s: if c ==...这样呢?