Python:如何删除字符串中的某些单词
我有以下字符串: 那么宝藏就在我们留下的那个岛上 当你们离开我的时候 我的任务是删除除重复出现的单词以外的所有内容。因此,输出将是:Python:如何删除字符串中的某些单词,python,string,Python,String,我有以下字符串: 那么宝藏就在我们留下的那个岛上 当你们离开我的时候 我的任务是删除除重复出现的单词以外的所有内容。因此,输出将是: >>>先右后左 我已成功获得以下输出: >>>[“右”、“然后”、“宝藏”、“是”、“在”、“那个”、“岛”、“在哪里”、“我们”、“左”、“它”、“什么时候”、“叶”、“我”] 现在,如果我设法从原始字符串中删除上述单词,我将保留正确的输出。如何操作?使用计数器: from collections import Counter words = Counter('
>>>先右后左
我已成功获得以下输出:
>>>[“右”、“然后”、“宝藏”、“是”、“在”、“那个”、“岛”、“在哪里”、“我们”、“左”、“它”、“什么时候”、“叶”、“我”]
现在,如果我设法从原始字符串中删除上述单词,我将保留正确的输出。如何操作?使用
计数器
:
from collections import Counter
words = Counter('right then the treasure be right on that island right where we left it then when ye left me')
for word, count in words.most_common():
if count > 1:
print word,
这将提供所需的输出,打印字符串中计数大于1的单词的所有当前值的列表
your_string = 'right then the treasure be right on that island right where we left it then when ye left me'
words = your_string.split()
words_that_occur_more_than_once = [word for word in words if words.count(word)>1]
words_that_occur_more_than_once.sort(reverse=True)
print(' '.join(words_that_occur_more_than_once))
这将按您想要的方式排列字符串。我不明白为什么输出中应该少一个“正确”字。首先创建一个方法来确定字符串是否显示多次。然后使用该方法提取您想要的单词,然后重新打印
# Method returns True if the searchWord is in the text only once.
# Returns False otherwise.
def occursOnlyOnce(searchWord, text):
count = 0
words = text.split();
for word in words:
if word == searchWord:
count += 1
if count == 1:
return True
else:
return False
# Initial input text.
initialText = "right then the treasure be right on that island right where we left it then when ye left me"
# Split words into a list.
words = initialText.split();
# List of recurring words only.
finalWords = []
# Extract words that don't appear once, and place into a list.
for word in words:
if not occursOnlyOnce(word, initialText):
finalWords.append(word)
# Assemble a space-delimited string containing the words.
finalStr = " ".join(finalWords)
# Print out the words that occur more than once.
print(finalStr)
此代码将正确删除所有内容并删除额外的空格
def removePhrase(string, charArray):
string = str(string)
text = string.split()
for x in range (0, len(text)):
for y in range (0, len(charArray)):
if text[x] == charArray[y]:
text[x] = text[x].replace(charArray[y], "")
text = " ".join(text)
return text
string = "right then the treasure be right on that island right where we left it then when ye left me"
text = string.split()
charArray = [word for word in text if text.count(word)<2]
print(charArray)
finalString = removePhrase(string, charArray)
finalString = ' '.join(finalString.split())
print(finalString)
def removephase(字符串,字符):
string=str(string)
text=string.split()
对于范围(0,长度(文本))中的x:
对于范围(0,len(charArray))中的y:
如果文本[x]==字符[y]:
文本[x]=文本[x]。替换(字符[y],“”)
text=”“.join(文本)
返回文本
string=“那么宝藏就在那个岛上,就在你们离开我的时候,我们把它放在那里了”
text=string.split()
charArray=[如果text.count(word)在这个意义上,您的输出应该是:-然后右-然后左。我对吗?所需的输出是“右-右-然后左”这还不清楚是应该包含所有发生的事件还是只包含一次:O即使在assembler中,对于初学者来说,对于这样一个琐碎的任务,我们也可以使用更少的代码,这样更容易理解分解为更小的逻辑步骤的代码,并且注释良好。当我在Python 3.4.1中运行您的代码时,会出现以下错误print(出现次数超过一次的单词。join(“”))AttributeError:“list”对象没有属性“join”
。它应该是“”。join(出现次数超过一次的单词)
这给了我输出:右然后右-左-左-左-输出应该是:右-右然后左-规则是什么,然后?右在字符串中有三次…如果您想订购,可以返回“右-右-右-左-左”。您的返回“右-右-左”