在自然语言处理、数据科学和数据挖掘等领域,我们需要处理大量的文本数据。为此,我们通常在Python中使用字符串和列表。给定一个字符列表或一个字符串,我们有时需要从列表或字符串中删除一个或所有出现的字符。在这篇文章中,我们将讨论在 python 中从一个列表或字符串中删除所有出现的字符的不同方法。
目录
使用 pop() 方法从一个列表中删除一个元素
给定一个列表,我们可以使用pop() 方法从列表中删除一个元素。pop() 方法,当在一个列表上调用时,将从列表中删除最后一个元素。执行后,它返回被删除的元素。你可以在下面的例子中观察到这一点。
myList = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", myList)
x = myList.pop()
print("The popped element is:", x)
print("The modified list is:", myList)
输出。
The original list is: [1, 2, 3, 4, 5, 6, 7]
The popped element is: 7
The modified list is: [1, 2, 3, 4, 5, 6]
在上面的例子中,你可以看到在执行pop() 方法后,列表的最后一个元素已经从列表中被删除。
然而,如果输入列表是空的,程序将运行到IndexError 。这意味着你正试图从一个空列表中弹出一个元素。例如,请看下面的例子。
myList = []
print("The original list is:", myList)
x = myList.pop()
print("The popped element is:", x)
print("The modified list is:", myList)
输出。
The original list is: []
Traceback (most recent call last):
File "/home/aditya1117/PycharmProjects/pythonProject/string12.py", line 3, in <module>
x = myList.pop()
IndexError: pop from empty list
你可以观察到,程序遇到了一个IndexError 异常,信息是 "IndexError: pop from empty list"。
你也可以使用pop() 方法从列表中的特定索引中删除元素。为此,你将需要提供该元素的索引。
pop() 方法,当在一个列表上调用时,将需要删除的元素的索引作为其输入参数。执行后,它将从给定的索引中删除该元素。pop() 方法也返回被删除的元素。你可以在下面的例子中观察到这一点。
myList = [1, 2, 3, 4, 5, 6, 7, 8]
print("The original list is:", myList)
x = myList.pop(3)
print("The popped element is:", x)
print("The modified list is:", myList)
输出。
The original list is: [1, 2, 3, 4, 5, 6, 7, 8]
The popped element is: 4
The modified list is: [1, 2, 3, 5, 6, 7, 8]
在这里,我们使用pop() 方法在列表的索引3处弹出了一个元素。
使用remove()方法从列表中删除一个元素
如果你不知道要删除的元素的索引,你可以使用 remove() 方法。remove() 方法,当在一个列表上调用时,需要一个元素作为其输入参数。执行后,它将从列表中删除输入元素的第一次出现。remove() 方法并不返回任何值。换句话说,它返回None 。
你可以在下面的例子中观察到这一点。
myList = [1, 2, 3, 4,3, 5,3, 6, 7, 8]
print("The original list is:", myList)
myList.remove(3)
print("The modified list is:", myList)
输出。
The original list is: [1, 2, 3, 4, 3, 5, 3, 6, 7, 8]
The modified list is: [1, 2, 4, 3, 5, 3, 6, 7, 8]
在上面的例子中,你可以看到在执行remove() 方法后,第1次出现的元素3被从列表中移除。
如果remove() 方法的输入参数中给出的值在列表中不存在,程序将遇到一个ValueError 异常,如下图所示。
myList = []
print("The original list is:", myList)
myList.remove(3)
print("The modified list is:", myList)
输出。
The original list is: []
Traceback (most recent call last):
File "/home/aditya1117/PycharmProjects/pythonProject/string12.py", line 3, in <module>
myList.remove(3)
ValueError: list.remove(x): x not in list
在上面的例子中,列表是空的。因此,数字3并不是列表中的一个元素。因此,当我们调用remove() 方法时,程序会遇到ValueError 异常,信息是 "ValueError: list.remove(x): x not in list"。
到现在为止,我们已经讨论了如何从一个列表中删除一个元素。现在让我们讨论一下如何在 python 中删除一个字符列表中的所有出现的字符。
从列表中删除一个字符的所有出现次数
给定一个字符列表,我们可以使用for循环和append() 方法删除所有出现的值。为此,我们将使用以下步骤。
- 首先,我们将创建一个名为
outputList的空列表。为此,你可以使用方括号或list()构造函数。 - 在创建
outputList,我们将使用for循环遍历输入的字符列表。 - 在遍历列表中的元素时,我们将检查是否需要删除当前的字符。
- 如果是,我们将使用continue语句移到下一个字符。否则,我们将使用
append()方法将当前字符追加到outputList。
在执行for循环后,我们将在outputList中得到输出的字符列表。在outputList中,除了那些我们需要从原始列表中删除的字符外,所有的字符都将存在。你可以在下面的python程序中观察到这一点。
myList = ['p', 'y', 'c', 't', 'c', 'h', 'o', 'n', 'f', 'c', 'o', 'r', 'b', 'e', 'g', 'c', 'i', 'n', 'n', 'c', 'e', 'r',
's']
print("The original list is:", myList)
outputList = []
charToDelete = 'c'
print("The character to be removed is:", charToDelete)
for character in myList:
if character == charToDelete:
continue
outputList.append(character)
print("The modified list is:", outputList)
输出。
The original list is: ['p', 'y', 'c', 't', 'c', 'h', 'o', 'n', 'f', 'c', 'o', 'r', 'b', 'e', 'g', 'c', 'i', 'n', 'n', 'c', 'e', 'r', 's']
The character to be removed is: c
The modified list is: ['p', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
使用列表理解法从列表中删除一个字符的所有出现次数
我们可以使用列表理解和成员操作符 'in' 来删除一个给定字符的所有出现,而不是使用for循环。
in 运算符是一个二进制运算符,它把一个元素作为它的第一个操作数,把一个容器对象如列表作为它的第二个操作数。执行后,如果该元素存在于容器对象中,则返回True 。否则,它将返回False 。你可以在下面的例子中观察到这一点。
myList = [1, 2, 3, 4,3, 5,3, 6, 7, 8]
print("The list is:", myList)
print(3 in myList)
print(1117 in myList)
输出。
The list is: [1, 2, 3, 4, 3, 5, 3, 6, 7, 8]
True
False
使用列表理解和 'in' 操作符,我们可以创建一个新的列表,其中有所有的字符,除了那些我们需要从原始列表中删除的字符,如下面的例子所示。
myList = ['p', 'y', 'c', 't', 'c', 'h', 'o', 'n', 'f', 'c', 'o', 'r', 'b', 'e', 'g', 'c', 'i', 'n', 'n', 'c', 'e', 'r', 's']
print("The original list is:", myList)
charToDelete = 'c'
print("The character to be removed is:", charToDelete)
outputList = [character for character in myList if character != charToDelete]
print("The modified list is:", outputList)
输出。
The original list is: ['p', 'y', 'c', 't', 'c', 'h', 'o', 'n', 'f', 'c', 'o', 'r', 'b', 'e', 'g', 'c', 'i', 'n', 'n', 'c', 'e', 'r', 's']
The character to be removed is: c
The modified list is: ['p', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
使用remove()方法从列表中删除一个元素的所有出现次数
与其创建一个新的列表,我们还可以从原始列表中删除一个字符的所有实例。为此,我们将使用remove() 方法和成员操作符 'in'。
- 为了从给定的字符列表中删除一个给定元素的所有实例,我们将使用'
in' 操作符检查该字符是否存在于列表中。如果是,我们将使用remove方法从列表中删除该字符。 - 我们将使用一个while循环来重复检查该字符是否存在,以便将其从列表中删除。
- 在删除所有给定字符的出现后,程序将从while循环中退出。
通过这种方式,我们将通过修改原始列表得到输出列表,如下所示。
myList = ['p', 'y', 'c', 't', 'c', 'h', 'o', 'n', 'f', 'c', 'o', 'r', 'b', 'e', 'g', 'c', 'i', 'n', 'n', 'c', 'e', 'r',
's']
print("The original list is:", myList)
charToDelete = 'c'
print("The character to be removed is:", charToDelete)
while charToDelete in myList:
myList.remove(charToDelete)
print("The modified list is:", myList)
输出。
The original list is: ['p', 'y', 'c', 't', 'c', 'h', 'o', 'n', 'f', 'c', 'o', 'r', 'b', 'e', 'g', 'c', 'i', 'n', 'n', 'c', 'e', 'r', 's']
The character to be removed is: c
The modified list is: ['p', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
使用filter()函数删除一个字符的所有出现次数
我们还可以使用filter() 函数来删除字符列表中所有出现的字符。
filter() 函数将另一个函数(如myFun )作为其第一个输入参数,将一个容器对象(如列表)作为其第二个参数。这里,myFun 应该把容器对象的一个元素作为它的输入参数。执行后,它应该返回True 或False 。
如果myFun 的输出是True ,对于输入中给出的容器对象的任何元素,该元素被包含在输出中。否则,这些元素不包括在输出中。
为了使用filter() 方法删除一个列表中给定项目的所有出现,我们将遵循以下步骤。
- 首先,我们将定义一个函数
myFun,它以一个字符作为输入参数。如果输入的字符等于我们需要删除的字符,它将返回False。否则,它应该返回True。 - 在定义了
myFun之后,我们将把myFun作为第一个参数,把字符列表作为第二个输入参数传给filter()函数。 - 执行后,
filter()函数将返回一个可迭代对象,其中包含未从列表中删除的字符。 - 为了将可迭代对象转换为列表,我们将把可迭代对象传递给
list()构造函数。通过这种方式,我们将得到删除所有指定字符出现后的列表。
你可以在下面的例子中观察整个过程。
def myFun(character):
charToDelete = 'c'
return charToDelete != character
myList = ['p', 'y', 'c', 't', 'c', 'h', 'o', 'n', 'f', 'c', 'o', 'r', 'b', 'e', 'g', 'c', 'i', 'n', 'n', 'c', 'e', 'r',
's']
print("The original list is:", myList)
charToDelete = 'c'
print("The character to be removed is:", charToDelete)
outputList=list(filter(myFun,myList))
print("The modified list is:", outputList)
输出。
The original list is: ['p', 'y', 'c', 't', 'c', 'h', 'o', 'n', 'f', 'c', 'o', 'r', 'b', 'e', 'g', 'c', 'i', 'n', 'n', 'c', 'e', 'r', 's']
The character to be removed is: c
The modified list is: ['p', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
我们可以不定义函数myFun ,而是创建一个lambda函数,并将其传递给过滤器函数,以从列表中删除所有字符的实例。你可以这样做。
myList = ['p', 'y', 'c', 't', 'c', 'h', 'o', 'n', 'f', 'c', 'o', 'r', 'b', 'e', 'g', 'c', 'i', 'n', 'n', 'c', 'e', 'r',
's']
print("The original list is:", myList)
charToDelete = 'c'
print("The character to be removed is:", charToDelete)
outputList = list(filter(lambda character: character != charToDelete, myList))
print("The modified list is:", outputList)
输出。
The original list is: ['p', 'y', 'c', 't', 'c', 'h', 'o', 'n', 'f', 'c', 'o', 'r', 'b', 'e', 'g', 'c', 'i', 'n', 'n', 'c', 'e', 'r', 's']
The character to be removed is: c
The modified list is: ['p', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
到目前为止,我们已经讨论了不同的方法来删除列表中出现的所有字符。现在我们将讨论如何从 python 字符串中删除一个特定字符的出现。
在Python中删除一个字符串中出现的所有字符
给定一个输入字符串,我们可以使用不同的字符串方法和正则表达式方法从字符串中删除一个或所有出现的字符。让我们逐一讨论这些方法。
在Python中使用for循环删除字符串中出现的所有字符
为了使用for循环从一个字符串中删除所有出现的特定字符,我们将遵循以下步骤。
- 首先,我们将创建一个名为
outputString的空字符串来存储输出字符串。 - 之后,我们将迭代原始字符串中的字符。
- 在遍历字符串中的字符时,如果我们发现需要从字符串中删除的字符,我们将使用continue语句移动到下一个字符。
- 否则,我们将把当前字符连接到
outputString。
在使用上述步骤使用for循环迭代到字符串的最后一个字符后,我们将在新的字符串中得到输出字符串,名为outputString 。你可以在下面的代码例子中观察到。
myStr = "pyctchonfcorbegcinncers"
print("The original string is:", myStr)
charToDelete = 'c'
print("The character to delete:", charToDelete)
outputString = ""
for character in myStr:
if character == charToDelete:
continue
outputString += character
print("The modified string is:", outputString)
输出。
The original string is: pyctchonfcorbegcinncers
The character to delete: c
The modified string is: pythonforbeginners
在Python中使用列表理解法从一个字符串中删除所有出现的字符
我们可以使用列表理解和 join() 方法从一个给定的字符串中删除一个特定值的出现,而不是使用for循环。
- 首先,我们将使用列表理解法创建一个不需要删除的字符串的字符列表,如下所示。
myStr = "pyctchonfcorbegcinncers"
print("The original string is:", myStr)
charToDelete = 'c'
print("The character to delete:", charToDelete)
myList = [character for character in myStr if character != charToDelete]
print("The list of characters is:")
print(myList)
输出。
The original string is: pyctchonfcorbegcinncers
The character to delete: c
The list of characters is:
['p', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
- 获得列表后,我们将使用
join()方法来创建输出列表。join()方法在调用一个特殊字符时,需要一个包含字符或字符串的可迭代对象作为其输入参数。执行后,它返回一个字符串。输出的字符串包含输入的可迭代对象中的字符,这些字符被调用连接方法的特殊字符分开。 - 我们将使用空字符
“”作为特殊字符。我们将在空字符上调用join()方法,并将从上一步获得的列表作为其输入参数。在执行join()方法后,我们将得到想要的输出字符串。 你可以在下面的例子中观察到这一点。
myStr = "pyctchonfcorbegcinncers"
print("The original string is:", myStr)
charToDelete = 'c'
print("The character to delete:", charToDelete)
myList = [character for character in myStr if character != charToDelete]
print("The list of characters is:")
print(myList)
outputString = "".join(myList)
print("The modified string is:", outputString)
输出。
The original string is: pyctchonfcorbegcinncers
The character to delete: c
The list of characters is:
['p', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
The modified string is: pythonforbeginners
在Python中使用split()方法从一个字符串中删除一个字符的所有出现次数
我们也可以使用split() 方法从一个给定的字符串中删除一个字符的所有出现。split() 方法,当对一个字符串调用时,需要一个分隔符作为其输入参数。执行后,它返回一个由分隔符分隔的子字符串的列表。
为了从一个给定的字符串中删除所有出现的字符,我们将使用以下步骤。
- 首先,我们将在原始字符串上调用
split()方法。我们将把需要删除的字符作为输入参数传递给split()方法。我们将把split()方法的输出存储在myList。 - 在获得
myList之后,我们将在一个空字符串上调用join()方法,以myList作为join()方法的输入参数。 - 在执行
join()方法后,我们将得到想要的输出。因此,我们将把它存储在一个变量outputString。
你可以在下面的例子中观察整个过程。
myStr = "pyctchonfcorbegcinncers"
print("The original string is:", myStr)
charToDelete = 'c'
print("The character to delete:", charToDelete)
myList = myStr.split(charToDelete)
print("The list is:")
print(myList)
outputString = "".join(myList)
print("The modified string is:", outputString)
输出。
The character to delete: c
The list is:
['py', 't', 'honf', 'orbeg', 'inn', 'ers']
The modified string is: pythonforbeginners
在Python中使用filter()函数从一个字符串中删除所有出现的一个字符
我们也可以使用 filter() 函数与join() 方法和 lambda 函数来从Python中的一个字符串中删除一个字符的出现次数。
filter() 函数将另一个函数(如myFun )作为它的第一个输入参数,将一个像字符串一样的可迭代对象作为它的第二个输入参数。这里,myFun 应该把字符串对象的字符作为它的输入参数。执行后,它应该返回True 或False 。
如果myFun 的输出是True ,对于输入中给出的字符串对象的任何一个字符,该字符被包含在输出中。否则,这些字符不包括在输出中。
为了删除一个字符串中出现的所有字符,我们将遵循以下步骤。
- 首先,我们将定义一个函数
myFun,将一个字符作为输入参数。如果输入的字符与要删除的字符相同,它将返回False。否则,它应该返回True。 - 在定义了
myFun之后,我们将把myFun作为第一个参数,把字符串作为第二个输入参数传给filter()函数。 - 执行后,
filter()函数将返回一个可迭代的对象,其中包含未从字符串中删除的字符。 - 我们将通过把可迭代对象传递给
list()构造函数来创建一个字符列表。 - 一旦我们得到字符列表,我们将创建输出字符串。为此,我们将在一个空字符串上调用
join()方法,将字符列表作为其输入参数。 - 在执行
join()方法后,我们将得到想要的字符串。
你可以在下面的代码中观察整个过程。
def myFun(character):
charToDelete = 'c'
return charToDelete != character
myStr = "pyctchonfcorbegcinncers"
print("The original string is:", myStr)
charToDelete = 'c'
print("The character to delete:", charToDelete)
myList = list(filter(myFun, myStr))
print("The list is:")
print(myList)
outputString = "".join(myList)
print("The modified string is:", outputString)
输出。
The original string is: pyctchonfcorbegcinncers
The character to delete: c
The list is:
['p', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
The modified string is: pythonforbeginners
我们可以不定义函数myFun ,而是创建一个等价的lambda函数,并将其传递给过滤器函数,以便从字符串中删除所有的字符实例。你可以这样做,如下。
myStr = "pyctchonfcorbegcinncers"
print("The original string is:", myStr)
charToDelete = 'c'
print("The character to delete:", charToDelete)
myList = list(filter(lambda character: character != charToDelete, myStr))
print("The list is:")
print(myList)
outputString = "".join(myList)
print("The modified string is:", outputString)
输出。
The original string is: pyctchonfcorbegcinncers
The character to delete: c
The list is:
['p', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
The modified string is: pythonforbeginners
在Python中使用replace()方法从一个字符串中删除一个字符的所有出现次数
replace() 方法,当对一个字符串调用时,将需要替换的字符作为它的第一个参数。在第二个参数中,它接收将被替换的字符,以代替第一个参数中给出的原始字符。
执行后,replace() 方法返回一个作为输入的字符串的副本。在输出的字符串中,所有的字符都被替换成了新的字符。
为了从一个字符串中删除所有给定的字符,我们将对字符串调用replace() 方法。我们将把需要删除的字符作为第一个输入参数。在第二个输入参数中,我们将传递一个空字符串。
执行后,所有出现的字符将被一个空字符串取代。因此,我们可以说该字符已经从字符串中被删除。
你可以在下面的例子中观察整个过程。
myStr = "pyctchonfcorbegcinncers"
print("The original string is:", myStr)
charToDelete = 'c'
print("The character to delete:", charToDelete)
outputString = myStr.replace(charToDelete, "")
print("The modified string is:", outputString)
输出。
The original string is: pyctchonfcorbegcinncers
The character to delete: c
The modified string is: pythonforbeginners
在Python中使用translate()方法从一个字符串中删除所有出现的一个字符
我们也可以使用 translate() 方法从一个字符串中删除字符。translate() 方法,当对一个字符串调用时,需要一个翻译表作为输入参数。执行后,它返回一个根据翻译表修改的字符串。
翻译表可以用maketrans() 方法创建。maketrans() 方法,当对一个字符串调用时,将需要替换的字符作为其第一个参数,将新字符作为其第二个参数。执行后,它返回一个翻译表。
我们将使用以下步骤从字符串中删除给定的字符。
- 首先,我们将在输入字符串上调用
maketrans()方法。我们将把需要删除的字符作为第一个输入参数和一个空格字符作为第二个输入参数传给maketrans()方法。在这里,我们不能向maketrans()方法传递一个空字符,这样它就可以将该字符映射为空字符串。这是由于两个字符串参数的长度应该是一样的。否则,maketrans()方法就会出错。 - 执行后,
maketrans()方法将返回一个翻译表,将我们需要删除的字符映射为一个空格字符。 - 一旦我们得到翻译表,我们将在输入字符串上调用
translate()方法,翻译表是其输入参数。 - 执行后,
translate()方法将返回一个字符串,其中我们需要删除的字符被替换为空格字符。 - 为了删除字符串中的空格字符,我们将首先在
translate()方法的输出上调用split()方法。在这一步之后,我们将得到一个子字符串的列表。 - 现在,我们将在一个空字符串上调用
join()方法。在这里,我们将把子串列表作为输入传给join()方法。 - 在执行
join()方法后,我们将得到所需的字符串。
你可以在下面的代码中观察整个过程。
myStr = "pyctchonfcorbegcinncers"
print("The original string is:", myStr)
charToDelete = 'c'
print("The character to delete:", charToDelete)
translationTable = myStr.maketrans(charToDelete, " ")
outputString = "".join(myStr.translate(translationTable).split())
print("The modified string is:", outputString)
输出。
The original string is: pyctchonfcorbegcinncers
The character to delete: c
The modified string is: pythonforbeginners
在Python中使用正则表达式从一个字符串中删除所有出现的字符
正则表达式提供了处理字符串或文本数据的最有效方法之一。
要从一个字符串中删除一个字符,我们可以使用re 模块中定义的sub() 方法。 sub() 方法将需要替换的字符(如old_char )作为其第一个输入参数。它把新的字符new_char 作为第二个输入参数,并把输入的字符串作为第三个参数。执行后,它将输入字符串中的old_char 替换为new_char ,并返回一个新字符串。
为了从一个给定的字符串中删除所有出现的字符,我们将使用以下步骤。
- 我们将把需要删除的字符作为第一个输入参数
old_char传递给sub()方法。 - 作为第二个参数
new_char,我们将传递一个空字符串。 - 我们将把输入字符串作为第三个参数传给
sub()方法。
执行后,sub() 方法将返回一个新的字符串。在新的字符串中,需要删除的字符将被空字符串字符所取代。因此,我们将得到想要的输出字符串。
你可以在下面的代码中观察到这一点。
import re
myStr = "pyctchonfcorbegcinncers"
print("The original string is:", myStr)
charToDelete = 'c'
print("The character to delete:", charToDelete)
outputString = re.sub(charToDelete, "", myStr)
print("The modified string is:", outputString)
输出。
The original string is: pyctchonfcorbegcinncers
The character to delete: c
The modified string is: pythonforbeginners
结论
在这篇文章中,我们讨论了从一个列表中删除所有出现的字符的不同方法。同样地,我们也讨论了删除字符串中所有出现的字符的不同方法。对于列表,我建议你使用remove() 的方法。对于字符串,你可以使用replace() 方法或re.sub() 方法,因为这些是在 python 中删除列表或字符串中出现的所有字符的最有效方法。
我希望你喜欢阅读这篇文章。请继续关注更多内容丰富的文章。
学习愉快!