Python|从字符串中删除不需要的字符
程序员面临的一般问题是从整个字符串中删除一个字符。但有时要求远高于要求删除 1 个以上的字符,但要删除此类恶意字符的列表。这些可以是特殊字符的形式,用于重建有效密码和许多其他可能的应用程序。让我们讨论执行此特定任务的某些方法。
方法#1:使用replace()
可以在循环中使用 replace() 来检查 bad_char,然后用空字符串替换它,从而将其删除。这是最基本的方法,从性能的角度来看是低效的。
Python3
# Python3 code to demonstrate
# removal of bad_chars
# using replace()
# initializing bad_chars_list
bad_chars = [';', ':', '!', "*"]
# initializing test string
test_string = "Ge;ek * s:fo ! r;Ge * e*k:s !"
# printing original string
print ("Original String : " + test_string)
# using replace() to
# remove bad_chars
for i in bad_chars :
test_string = test_string.replace(i, '')
# printing resultant string
print ("Resultant list is : " + str(test_string))
Python3
# Python3 code to demonstrate
# removal of bad_chars
# using join() + generator
# initializing bad_chars_list
bad_chars = [';', ':', '!', "*"]
# initializing test string
test_string = "Ge;ek * s:fo ! r;Ge * e*k:s !"
# printing original string
print ("Original String : " + test_string)
# using join() + generator to
# remove bad_chars
test_string = ''.join(i for i in test_string if not i in bad_chars)
# printing resultant string
print ("Resultant list is : " + str(test_string))
Python3
# Python3 code to demonstrate
# removal of bad_chars
# using translate()
import string
# initializing bad_chars_list
bad_chars = [';', ':', '!', "*"]
# initializing test string
test_string = "Ge;ek * s:fo ! r;Ge * e*k:s !"
# printing original string
print ("Original String : " + test_string)
# using translate() to
# remove bad_chars
delete_dict = {sp_character: '' for sp_character in string.punctuation}
delete_dict[' '] = ''
table = str.maketrans(delete_dict)
test_string = test_string.translate(table)
# printing resultant string
print ("Resultant list is : " + str(test_string))
Python3
# Python3 code to demonstrate
# removal of bad_chars
# using filter()
# initializing bad_chars_list
bad_chars = [';', ':', '!', "*"]
# initializing test string
test_string = "Ge;ek*s:fo!r;Ge*e*k:s!"
# printing original string
print("Original String : " + test_string)
# using filter() to
# remove bad_chars
test_string = ''.join((filter(lambda i: i not in bad_chars, test_string)))
# printing resultant string
print("Resultant list is : " + str(test_string))
输出 :
Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks
方法 #2:使用 join() + 生成器
通过使用 join() 我们重新制作了字符串。在生成器函数中,我们指定了忽略 bad_chars 中的字符的逻辑,从而构造没有坏字符的新字符串。
Python3
# Python3 code to demonstrate
# removal of bad_chars
# using join() + generator
# initializing bad_chars_list
bad_chars = [';', ':', '!', "*"]
# initializing test string
test_string = "Ge;ek * s:fo ! r;Ge * e*k:s !"
# printing original string
print ("Original String : " + test_string)
# using join() + generator to
# remove bad_chars
test_string = ''.join(i for i in test_string if not i in bad_chars)
# printing resultant string
print ("Resultant list is : " + str(test_string))
输出 :
Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks
方法 #3:使用 translate()
执行这个特定任务的最优雅的方式,这个方法本身基本上是用来解决这类问题的,我们可以将每个 bad_char 转换为空字符串并得到过滤的字符串。
Python3
# Python3 code to demonstrate
# removal of bad_chars
# using translate()
import string
# initializing bad_chars_list
bad_chars = [';', ':', '!', "*"]
# initializing test string
test_string = "Ge;ek * s:fo ! r;Ge * e*k:s !"
# printing original string
print ("Original String : " + test_string)
# using translate() to
# remove bad_chars
delete_dict = {sp_character: '' for sp_character in string.punctuation}
delete_dict[' '] = ''
table = str.maketrans(delete_dict)
test_string = test_string.translate(table)
# printing resultant string
print ("Resultant list is : " + str(test_string))
输出 :
Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks
方法 #4:使用 filter()
这是执行此任务的另一种解决方案。使用 lambda函数,过滤函数可以删除所有的 bad_chars 并返回想要的精炼字符串。
Python3
# Python3 code to demonstrate
# removal of bad_chars
# using filter()
# initializing bad_chars_list
bad_chars = [';', ':', '!', "*"]
# initializing test string
test_string = "Ge;ek*s:fo!r;Ge*e*k:s!"
# printing original string
print("Original String : " + test_string)
# using filter() to
# remove bad_chars
test_string = ''.join((filter(lambda i: i not in bad_chars, test_string)))
# printing resultant string
print("Resultant list is : " + str(test_string))
输出 :
Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks