我是 python 新手,我的函式有問題。我需要從字串中洗掉標點符號,所以,我能想到的最簡單的方法是回圈字串并將標點符號替換為空字符;盡管該函式實際上確實洗掉了句號,但它并沒有使用逗號。我試圖除錯它,它識別出條件中的逗號,但它沒有洗掉它。
代碼是這樣的:
string = "the Zen of Python, by Tim Peters. beautiful is better than ugly. explicit is better than implicit. simple is better than complex."
def remove_punctuation(string):
punctuation = [",", "."]
for char in string:
if char in punctuation:
raw_string = string.replace(char, "")
return raw_string
print(remove_punctuation(string))
問題是練習說我只能使用替換或洗掉,所以我對此有點限制。
uj5u.com熱心網友回復:
解決方案1:
替換:
def remove_punctuation(string):
punctuation = [",", "."]
for char in punctuation:
string = string.replace(char, "")
return string
string = "the Zen of Python, by Tim Peters. beautiful is better than ugly." \
" explicit is better than implicit. simple is better than complex."
print(remove_punctuation(string))
基本上我們在punctuation.
解決方案2:
如果您想獲得更好的性能,您可以.translate使用以下字串:
def remove_punctuation(string):
punctuation = [",", "."]
table = str.maketrans(dict.fromkeys(punctuation))
return string.translate(table)
在翻譯中,表中值為 的每個鍵都None將從字串中洗掉。fromkeys將從一個可迭代物件創建一個字典并將None其作為它們的值(這是默認值)
uj5u.com熱心網友回復:
我建議使用正則運算式及其魔法來去除特殊字符:
import re
foo = "I'm a dirty string...$@@1##@*((#*@"
clean_foo = re.sub('\W ','', foo )
print(clean_foo) # Does not remove spaces (Outputs Imadirtystring1)
clean_foo2 = re.sub('[^A-Za-z0-9 ] ', '', foo)
print(clean_foo2) # Remove special chars only :D (Outputs IIm a dirty string1)
uj5u.com熱心網友回復:
您繼續替換標點符號一次,然后再次替換原始字串而不是修改后的字串。
試試這個:
def remove_punctuation(string):
punctuation = [",", "."]
for punc in punctuation:
string = string.replace(punc, "")
return string
print(remove_punctuation(string))
uj5u.com熱心網友回復:
您可以使用匯入標點符號,這是一個包含所有標點符號的串列。然后將句子中的每個字符添加到raw_stringif 它不在punctuation.
from string import punctuation
sentence = "the Zen of Python, by Tim Peters. beautiful is better than ugly. explicit is better than implicit. simple is better than complex."
def remove_punctuation(string):
raw_string = ""
for char in string:
if char not in punctuation:
raw_string = char
return raw_string
print(remove_punctuation(sentence))
uj5u.com熱心網友回復:
如果您希望得到最短和最快的答案,您可以使用翻譯表洗掉所有內容,如下所示:
>>> import string
>>> s = "the Zen of Python, by Tim Peters. beautiful is better than ugly. explicit is better than implicit. simple is better than complex."
>>> s.translate(str.maketrans("", "", string.punctuation))
'the Zen of Python by Tim Peters beautiful is better than ugly explicit is better than implicit simple is better than complex'
uj5u.com熱心網友回復:
在您的代碼中,您有 2 個回圈,內部回圈首先使用字串替換逗號,對于點,它再次使用相同的未修改值string。
因此,字串被用作源 2 次,并且替換點的第二次迭代的替換僅存盤在raw_string為您提供僅洗掉點的結果中。
您可以使用與 re.sub 匹配的字符類.或,與 re.sub匹配的單個呼叫
import re
string = "the Zen of Python, by Tim Peters. beautiful is better than ugly. explicit is better than implicit. simple is better than complex."
def remove_punctuation(string):
return re.sub(r"[,.]", "", string)
print(remove_punctuation(string))
輸出
the Zen of Python by Tim Peters beautiful is better than ugly explicit is better than implicit simple is better than complex
uj5u.com熱心網友回復:
您raw_string從頭開始在每次迭代中重新定義,因此您只能看到最后一次迭代的效果。您需要“累積”修改:
def remove_punctuation(string):
raw_string = string
punctuation = [",", "."]
for char in string:
if char in punctuation:
raw_string = raw_string.replace(char, "")
return raw_string
您也可以使用“串列”理解來做到這一點:
"".join(c for c in string if c not in punctuation)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/418868.html
標籤:
