Python中join()函数:在单词之间添加空格

5

我需要编写一个函数,该函数接受两个字符串(文本和单词),并将所选单词替换为星号(星号的数量应与被屏蔽单词中字母的数量相对应)。

例如:

如果text="hey hey hey",word="hey",则返回的文本应为:

'*** *** ***'

这是我的代码:
def censor(text,word):
    text = text.split(" ")
    asterisks=[]
    text_with_asterisks=[]

    for item in text:
        if item not in word:
            text_with_asterisks.append(item)
        else:
            asterisks=[]
            for letter in word:
                asterisks.append("*")

            text_with_asterisks.append(' '.join(asterisks))
    return (" ".join(text_with_asterisks))

代码可以运行,但是它返回了以下内容:
 ********* 

并非

*** *** ***. 

我使用下面的代码:

return ("_".join(text_with_asterisks))

我得到的是:
'***_***_***'

我不理解为什么“ ”被忽略了,我该如何在单词之间添加空格。
谢谢!

2
我得到的结果和你不一样。我得到的是 '* * * * * * * * *'。这是由于 text_with_asterisks.append(' '.join(asterisks)) 这一行代码导致的。如果我将其中的 ' ' 改为 '',那么我会得到 '*** *** ***' - TheBlackCat
2
你可以对字符串进行乘法操作:'*' * len(word) - Peter Wood
1
你需要使用 join 两次,第一次用于连接代替每个单词的星号,然后再次用于连接代替星号的单词。虽然你已经做到了,但是你需要在第一次使用 "" 作为分隔符,下一次使用 " " 作为分隔符。 - skyking
1
此外,您的代码并不像您期望的那样工作。它实际上是在寻找word中的单词。调用cencor("he", "hey"),您将得到***,因为hehey的一部分,并且三个星号,因为hey有三个字母。 - skyking
1
你的信息的第一部分表明你想要替换一个单词,但是你的示例程序似乎暗示你实际上有一个词汇列表需要审查。也许你应该澄清这一点。否则,像Adem Öztaş那样聪明的一行代码可能是你想要的。 - YvesQuemener
显示剩余2条评论
7个回答

3
当你连接星号时,多了一个空格:
def censor(text, word):
    text = text.split(" ")
    asterisks=[]
    text_with_asterisks=[]

    for item in text:
        if item not in word:
            text_with_asterisks.append(item)
        else:
            asterisks=[]
            for letter in word:
                asterisks.append("*")

            text_with_asterisks.append(''.join(asterisks)) #here's the culprit
    return (" ".join(text_with_asterisks))

censor("hey hey hey", "hey") 输出你想要的结果 ('*** *** ***')。

我只是指出了你的错误,但肯定有更优雅和高效的方法来实现你想要的功能。


非常感谢。现在它运行得很好。 不幸的是,我不被允许使用Regex方法或类似方法,但我同意有更优雅的解决方法。 - WhiteM

3
正则表达式实现的方法如下 -
import re
def censor(text,word):
    return re.sub(r'\b(?i){0}\b'.format(re.escape(word)),'*' * len(word), text)

示例/演示 -
>>> censor('hey hey they hey','hey')
'*** *** they ***'

>>> censor('hey hey they Hey','hey')
'*** *** they ***'

(?i)用于忽略大小写,re.escape(word)呢?否则,这正是我想要发布的内容。 - tobias_k
完成 - re.sub(r'\b(?i){0}\b'.format(re.escape(word)),'*' * len(word), text) - Anand S Kumar

3
这里是最简单的解决方案。
text.replace(word, "*" * len(word))

这也会替换掉单词的一部分,例如它会替换“masses”中的“ass”。最好使用带有单词边界标记的正则表达式。 - tobias_k
text.replace(" " + word + " " , "*" * len(word)) - 这有帮助吗? - dmr
那么标点呢? - tobias_k

2

您在单词中的每个*之间都有空格,并且单词之间还有额外的空格。因此,我认为您只想要单词之间的空格:

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

2

简单的解决方案,

>>> text = "hey hey hey"
>>> "***".join(text.split("hey"))
'*** *** ***'

或者

 >>> text = "hey hey they Hey','hey"  
 >>> " ".join([ '***' if word.lower() == 'hey' else word
 ... for word in text.replace("'","").replace(","," ").split()])
 '*** *** they *** ***'

1
正如text_with_asterisks.append(' '.join(asterisks))所做的那样,每个字符被" "连接起来,然后" ".join(text_with_asterisks)也使每个单词被" "连接起来,因此输出结果是:* * * * * * * * *,每个星号之间有一个空格。

0
def censor(text, censor_w):
    splitted_text = text.split(" ")
    asterics = "*" * len(censor_w)
    result = []

    for word in splitted_text:
        if word == censor:
            result.append(asterics)
        else:
            result.append(word)

    return " ".join(result)

虽然这段代码片段可能解决了问题,但包括解释真的有助于提高您的帖子质量。请记住,您正在为未来的读者回答问题,而这些人可能不知道您的代码建议原因。 - secelite

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接