Python 删除字符串中的K长度单词
在本文中,我们将学习一个Python程序,用于删除字符串中的K长度单词。
使用的方法
以下是完成此任务的各种方法:
- 使用split()、join()、len()和列表推导
-
使用filter()、lambda()、split()和len()函数
-
使用split()、join()、remove()和len()函数
示例
假设我们输入了一个字符串和一个k长度。我们将使用上述方法从输入字符串中删除所有给定的k长度单词。
输入
inputString = "hello tutorialspoint python codes"
k_length = 5
输出
Resultant string after removing 5 length words:
tutorialspoint python
在这个示例中,给定的 k 长度是 5 ,因此从输入字符串中删除所有长度为 5 的单词,如 hello, codes ,并打印结果字符串。
方法 1:使用split()、join()、len()和列表推导式
列表推导式
当你想基于现有列表的值构建一个新的列表时,列表推导式提供了一种更短、更简洁的语法。
join() − join() 是 Python 中的一个字符串函数,用于将以字符串分隔符分隔的序列元素连接起来。该函数连接序列元素以转换为字符串。
步骤
以下是执行所需任务的算法/步骤:
- 创建一个变量来存储 输入 字符串,并打印给定的字符串。
-
创建另一个变量来存储 k 长度。
-
使用 split() 函数 (将字符串分割成列表。我们可以定义分隔符;默认分隔符是任何空白字符)将输入字符串拆分为单词列表。
-
遍历单词列表,检查当前元素的长度是否不等于输入的 k 长度,并将其存储在一个列表中。
-
使用 join() 函数将列表转换为字符串。
-
打印从输入字符串中删除所有给定的 k 长度单词后的结果字符串。
示例
以下程序使用 split()、join()、len() 函数和列表推导式,在从输入字符串中删除所有给定的 k 长度单词后返回一个字符串 –
# input string
inputString = "hello tutorialspoint python codes"
# printing the input string
print("Input string: ", inputString)
# input k length value
k_length = 5
# splitting the input string into a list of words
wordsList = inputString.split()
# traversing through the words list and checking whether the length of
# current string is not equal to input k length and storing it in a list
resultList = [element for element in wordsList if len(element) != k_length]
# joining the above list to a string to convert it to a string
resultString = ' '.join(resultList)
# printing resultant string after removing k-length words
print("Resultant string after removing 5 length words:\n", resultString)
输出
在执行时,上述程序将生成以下输出 –
Input string: hello tutorialspoint python codes
Resultant string after removing 5 length words:
tutorialspoint python
方法2:使用filter()、lambda()、split()和len()函数
filter()函数 - 使用确定每个元素是真还是假的函数来过滤指定的序列。
Lambda函数
Lambda函数,通常称为’ 匿名函数 ‘,与普通的Python函数相同,只是它可以在没有名称的情况下定义。 def 关键字用于定义普通函数,而 lambda 关键字用于定义匿名函数。然而,它们限制在一行表达式上。和普通函数一样,它们可以接受多个参数。
语法
lambda arguments: expression
示例
以下程序使用filter()、lambda()、split()和len()函数从输入的字符串中删除所有输入的k长度的单词,并返回一个字符串-
# input string
inputString = "hello tutorialspoint python codes"
# printing the input string
print("Input string: ", inputString)
# input k length value
k_length = 5
# Splitting the input string into a list of words
wordsList = inputString.split()
# Traverse in the given words list and filter the list elements
# if the length of the element is not equal to k using the lambda function.
resultList = filter(lambda element: len(element) != k_length, wordsList)
# Converting filter object to the list
resultList = list(resultList)
#joining the above list to a string to convert it to a string
resultString = ' '.join(resultList)
# printing resultant string after removing k-length words
print("Resultant string after removing 5 length words:\n", resultString)
输出
在执行之后,上述程序将生成以下输出 –
Input string: hello tutorialspoint python codes
Resultant string after removing 5 length words:
tutorialspoint python
方法3:使用split()、join()、remove()和len()函数
split() - 将字符串分割成列表。我们可以定义分隔符;默认的分隔符是任何空白字符。
remove()方法 - 移除具有给定值的第一个元素。
语法
list.remove(obj)
示例
以下程序使用split()、join()、remove()和len()函数,从输入字符串中删除所有输入的长度为k的单词后返回一个字符串:
# input string
inputString = "hello tutorialspoint python codes"
# printing the input string
print("Input string: ", inputString)
# input k length value
k_length = 5
# splitting the input string into a list of words
wordsList = inputString.split()
# making a copy of the words list and traveling in that list
for element in wordsList.copy():
# checking whether the length of current is equal to the input k length
if len(element) == k_length:
# removing that element if the condition is true
wordsList.remove(element)
# joining words list to string to convert to a string
resultString = ' '.join(wordsList)
# printing resultant string after removing k-length words
print("Resultant string after removing 5 length words:\n", resultString)
输出
执行上述程序后,将生成以下输出 –
Input string: hello tutorialspoint python codes
Resultant string after removing 5 length words:
tutorialspoint python
结论
在本文中,我们学习了如何使用三种不同的方法从给定的单词列表中删除长度为k的术语。使用lambda函数和filter函数,我们还学会了如何根据指定的条件过滤列表的元素。