2013-05-21 141 views
0

我試圖做一個簡單的程序,它的文本的字符串和文字的列表,並打印文本,但與對應的字母許多的X.替換的話在這個詞裏。使用字典檢查文本字符串並用X替換單詞。 Python的

問題:我的代碼也會替換部分匹配l中的單詞的單詞。我怎樣才能讓它只瞄準整個單詞?

def censor(t, l): 

    for cenword in l: 
     number_of_X = len(cenword) 
     sensurliste = {cenword : ("x"*len(cenword))} 

     for cenword, x in sensurliste.items(): 
      word = t.replace(cenword, x) 
      t = word.replace(cenword, x) 

    print (word) 
+0

clbuttic錯誤 – mata

+0

@mata護理闡述?編程新手。 – user2406501

+0

[谷歌它](https://www.google.com/search?q=clbuttic+mistake):) – mata

回答

1

首先,我相信你想讓你的for循環在同一水平上,以便當一個完成其他的開始。其次,它看起來像你有額外的代碼,它並沒有真正做任何事情。

例如,sensurliste將只會存在與「X」字符串配對的刪減單詞。因此,第一個for循環是不需要的,因爲在第二個for循環中僅在當場創建「X」字符串是微不足道的。

然後,你說 字= t.replace(cenword,X) T = word.replace(cenword,X)

第二行不執行任何操作,因爲word已經取代cenword的所有實例。因此,這可以縮短到僅僅

t = t.replace(cenword,x); 

最後,這就是你的問題是,蟒蛇替代方法不關心單詞邊界。所以它將取代所有cenword的實例,無論它是否是一個完整的單詞。

你可以使用正則表達式來使它所以它只會找到完整的單詞的實例,但是,我只想用更多的東西沿着

def censort(t,l): 
    words = t.split()      #split the words into a list 
    for i in range(len(words)):    #for each word in the text 
     if words[i] in l:      #if it needs to be censoredx 
      words[i] = "X"*len(words[i])   #replace it with X's 
    t=words.join()       #rejoin the list into a string 
+0

它確實對字典有點尷尬。謝謝。 – user2406501

+0

只是一個註釋,它看起來像這種方法將錯過檢察詞的複數化。 – Andenthal

+0

另一個說明它也不會節省原始間距 – DanChianucci

0

線您可以使用正則表達式(模塊重新)用於替換,或將輸入字符串拆分爲您認爲是「整個單詞」的內容。

如果你考慮任何分離的空白是一個字,你可以做到以下幾點:

def censor(t, l): 
    for cenword in l: 
     number_of_X = len(cenword) 
     sensurliste = {cenword : ("x"*len(cenword))} 
    censored = [] 
    for word in t.split(): 
     append(sensurliste.get(word, word)) 
    return ' '.join(censurliste) 

請注意,這不保存原來的間距。此外,如果您的文字包含標點,這可能不會產生您認爲應該的內容。例如,如果t包含單詞'stupid!',但該列表只有'愚蠢',則不會被替換。

如果您想解決所有這些問題,您需要執行tokenisation。您可能還需要考慮大寫字母。

1

這樣做是使用正則表達式來獲取所有單詞的另一種方式:

import re 

blacklist = ['ccc', 'eee'] 

def replace(match): 
    word = match.group() 
    if word.lower() in blacklist: 
     return 'x' * len(word) 
    else: 
     return word 

text = 'aaa bbb ccc. ddd eee xcccx.' 

text = re.sub(r'\b\w*\b', replace, text, flags=re.I|re.U) 
print(text) 

這樣做的好處工作機智各類詞邊界正則表達式識別的。

1

這是很容易理解和清潔

def censor(text, word): 
     return text.replace(word, ("*"*len(word))) 
+1

他的第二個參數是一個單詞列表,而不是一個單詞。 – kingdamian42

相關問題