A basic python function to censor replace words in sentences

时间:2015-07-28 23:39:41

标签: python

I am trying to build a simple function to censor a specific word in a sentence. For example "hello hello hi" to "***** ***** hi" if I feed censor("hello hello hi", "hello"). Assuming I will not receive punctuation and sentences with upper case letters or empty strings.

After researching online I understand there is simpler solution, for example:

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

I still want to understand from a learning perspective, what I did wrong in the below more complicated code.

def censor(text,word):
    split_text = text.split()
    length = len(word)
    for item in split_text:
        if item == word:
            item = ("*" * length)
        else:
            item = item
    return " ".join(split_text)

5 个答案:

答案 0 :(得分:1)

The for-loop in your code does not reference or modify the actual item in 2015-07-28 19:40:46,225 INFO CoordActionInputCheckXCommand:539 - USER[-] GROUP[-] TOKEN[-] APP[-] JOB[0131647-140520191754742-oozie-oozi-C] ACTION[0131647-140520191754742-oozie-oozi-C@2] [0131647-140520191754742-oozie-oozi-C@2]::ActionInputCheck:: In checkResolvedUris... 2015-07-28 19:40:46,225 INFO CoordActionInputCheckXCommand:539 - USER[-] GROUP[-] TOKEN[-] APP[-] JOB[0131647-140520191754742-oozie-oozi-C] ACTION[0131647-140520191754742-oozie-oozi-C@2] [0131647-140520191754742-oozie-oozi-C@2]::ActionInputCheck:: In checkListOfPaths: /donemarkers/dependency-job/20150725.done is Missing. 2015-07-28 19:40:46,241 INFO CoordActionInputCheckXCommand:539 - USER[-] GROUP[-] TOKEN[-] APP[-] JOB[0131647-140520191754742-oozie-oozi-C] ACTION[0131647-140520191754742-oozie-oozi-C@2] [0131647-140520191754742-oozie-oozi-C@2]::ActionInputCheck:: File:/donemarkers/dependency-job/20150725.done, Exists? :true . Try this:

split_text

Also, it is generally considered poor practice to modify a list within a loop. See this post: How to modify list entries during for loop?

答案 1 :(得分:1)

You probably see what you did wrong based on the comments. But you might not understand why (and based on your question I think you may be interested to know).

In python you can roughly divide the datatypes into 2 categories: mutable and immutable. The basic difference is that you can create references to mutable type variables whereas you can only pass around the values of immutable type variables. Refer to this question for examples.

In your case you are iterating through a list containing strings. Strings happen to be immutable. So when you change @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("/resources"); } it won't have any effect on the corresponding element in item.

If instead you were iterating through a list of lists, you would actually be able to change the list elements using the placeholder variable.

Illustrated with an example:

split_text

Hope this helps! :)

答案 2 :(得分:1)

As mentioned other answers, you can't change list value in for loop.

You can access list value using import sys,platform from cx_Freeze import setup, Executable def getTargetName(): myOS = platform.system() if myOS == 'Linux': return "AppName" elif myOS == 'Windows': return "AppName.exe" else: return "AppName.dmg" base = None if sys.platform == "win32": base = "Win32GUI" exe = Executable(script = "main.py", base=base, targetName = getTargetName()) build_exe_options = {"packages": ["re", "sip"], "includes":["modules"], "icon":"icon.ico"} setup( name = "setup", version = "1.0", description = "GUI Application!", options = {"build_exe": build_exe_options}, executables = [exe]) , get index of value. And, you don't need enumerate because you don't change value.

else

答案 3 :(得分:1)

我提出的解决方案稍微简单一些,完成了工作。

1.我确保两个参数都是一个字符串,然后检查该单词是否在文本字符串中。 然后,如果单词在文本字符串中,我得到单词的长度。 然后我用字符串替换字符串中的单词,但这个单词很长。

def censor(text,word):
t=str(text)
w=str(word)
if w in t:
    l=len(w)
    item = ("*" * l)
    return t.replace(w,item)
else:
    return t

答案 4 :(得分:0)

There is a difference between the variable and the value that the variable has at a given point at the running time. The statement

sys.argv

Sequentially assigns the value of each item in for item in split_text: to split_text.

item

Changes item = ("*" * length) to a new value computed from its length. But item remains exactly what it was at the beginning.