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


方法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