Python 替换除给定单词外的所有单词
在本文中,我们将学习如何在Python中替换字符串中除给定单词外的所有单词。
使用的方法
以下是完成此任务的各种方法:
- 使用for循环、split()和join()函数
-
使用列表推导
示例
假设我们已经接收了一个输入字符串、输入的单词和要替换的输入字符。现在,我们将使用上面的方法将输入字符串中除给定单词外的所有单词替换为给定的替换字符。
输入
inputString = 'hello tutorialspoint python codes'
inputWord = "tutorialspoint"
replaceChar = "@"
输出
Replacing all words of string except input word with '@':
@ tutorialspoint @ @
在这个示例中,给定的输入单词是“ tutorialspoint ”。因此,除了给定的单词“tutorialspoint”外,输入字符串中的所有字符都被替换为 “@” 符号。
方法1:使用for循环,split()和join()函数
join() - join()是Python中的一个字符串函数,用于连接由字符串分隔符分隔的序列元素。此函数连接序列元素以转换为字符串。
split() 函数-将字符串拆分成列表。我们可以定义分隔符;默认的分隔符是任何空白字符。
步骤
以下是执行所需任务的算法/步骤。
- 创建一个变量来存储 输入字符串 。
-
打印输入字符串。
-
创建另一个变量来存储 输入单词 。
-
给出 替换字符 (替换为该字符)。
-
使用 split() 函数将输入字符串分割为单词列表。
-
使用for循环遍历单词列表的长度(使用 len() 函数返回对象中的项目数)。
-
获取当前索引处的列表元素。
-
使用 if条件 语句检查当前元素是否不等于输入单词(这里是tutorialspoint)。
-
如果条件为真,则用给定的替换字符替换当前元素。
-
使用 join()函数 将给定的单词列表转换为字符串。
-
打印替换掉输入字符串中除输入单词以外的所有单词的结果字符串。
示例
以下程序使用for循环、split()和join()函数,将输入字符串中除给定单词以外的所有单词替换为输入的替换字符(@)-
# input string
inputString = 'hello tutorialspoint python codes'
# printing input string
print("Input String:", inputString)
# input word
inputWord = "tutorialspoint"
# input replacing character (with which to be replaced)
replaceChar = "@"
# splitting input string into the list of words
wordsList = inputString.split(" ")
# traversing till the length of the words list
for index in range(len(wordsList)):
# getting the list element at current index
element = wordsList[index]
# checking whether the current element is not equal to the input word(tutorialspoint)
if not element == inputWord:
# assigning the input replacing character to the current element
# if the condition is true
wordsList[index] = replaceChar
# converting words list to a string using the join() function
resultantString = " ".join(wordsList)
# printing resultant string after replacing all words of the string
# except input word with input replacing character
print("Replacing all words of string except input word with '@':\n", resultantString)
输出
在执行时,上述程序将生成以下输出 –
Input String: hello tutorialspoint python codes
Replacing all words of string except input word with '@':
@ tutorialspoint @ @
时间复杂度 -O(n)
辅助空间 -O(n)
方法2:使用列表推导式
列表推导式
当你想要基于一个已有列表的值构建一个新的列表时,列表推导式提供了一种更短、更简洁的语法。
这是另一种完成这个任务的方法。在这种方法中,我们通过一个与之前方法功能类似的一行代码迭代元素并执行任务。
示例
以下程序使用列表推导式将输入字符串中除给定单词之外的所有单词替换为输入替换字符(*)-
# input string
inputString = 'hello tutorialspoint python codes'
# printing input string
print("Input String:", inputString)
# input word
inputWord = "python"
# input replacing character (with which to be replaced)
replaceChar = "*"
# Here we create a new list using list comprehension
# Where if the element(word) is equal to the input word then we add a word to the list
# Else we add replace character to the list
resultantList = [replaceChar if not element == inputWord else element for element in inputString.split()]
# Join the list of words with spaces to make it a string
resultantString = " ".join(resultantList)
# printing resultant string after replacing all words of string
# except input word with input replacing character
print("Replacing all words of string except input word with '*':\n", resultantString)
输出
执行上述程序后,将生成以下输出:
Input String: hello tutorialspoint python codes
Replacing all words of string except input word with '*':
* * python *
时间复杂度 − O(n)
辅助空间 − O(n)
结论
在本文中,我们学习了两种不同的方法来替换除指定单词之外的所有单词。我们还学习了如何使用列表推导来使用简单而简洁的语法完成任何任务。