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)
结论
在本文中,我们研究了两种不同的方法来替换除指定单词外的所有单词。我们还学习了如何使用列表推导式以简单简洁的语法来完成任何任务。
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP