如何去掉字符串中不需要的字符


方法1

方法2

方法3

改进版本

(因为上面的方法只能替换一个符号)

方法4

两种 translate()方法

string下的translate()

转变 a > x b > y c > z

构建映射表

结果


上述 table 若为None 说明不做任何的映射 第二个参数 表示 要删除字符串的指定的字符

unicode 下的translate()

问题三的解决方法



Python 字典(Dictionary) fromkeys()方法

#!/usr/bin/python

seq = ('name', 'age', 'sex')

dict = dict.fromkeys(seq)
print "New Dictionary : %s" %  str(dict)

dict = dict.fromkeys(seq, 10)
print "New Dictionary : %s" %  str(dict)


New Dictionary : {'age': None, 'name': None, 'sex': None}
New Dictionary : {'age': 10, 'name': 10, 'sex': 10}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM