我们正在计算机科学课上研究Vigenere密码,老师希望我们采取的第一步措施是删除字符串中的所有空格,标点符号和大写字母。
#pre-process - removing spaces, punctuation, and capitalization
def pre_process(s):
str = s.lower()
s = (str.replace(" ", "") + str.replace("!?'.", ""))
return s
print(pre_process("We're having a surprise birthday party for Eve!"))
我想要的输出是"werehavingasurpisebirthdaypartyforeve"
,但是我实际上得到的是"we'rehavingasurprisebirthdaypartyforeve!we're having a surprise birthday party for eve!"
答案 0 :(得分:1)
您应该使用正则表达式而不是字符串替换。试试这个代码。
setState
答案 1 :(得分:0)
您可以使用re
吗?,
>>> import re
>>> x
"We're having a surprise birthday party for Eve!"
>>> re.sub(r'[^a-zA-Z0-9]', '', x).lower() # negate the search. Fastest among the two :)
'werehavingasurprisebirthdaypartyforeve'
或list comprehension
吗?
>>> import string
>>> ''.join(y for y in x if y in string.ascii_letters).lower()
'werehavingasurprisebirthdaypartyforeve'
只是一个基准
>>> timeit.timeit("''.join(y for y in x if y in string.ascii_letters).lower()", setup='import string;x = "We\'re having a surprise birthday party for Eve!"')
7.747261047363281
>>> timeit.timeit("re.sub(r'[^a-zA-Z0-9]', '', x).lower()", setup='import re;x = "We\'re having a surprise birthday party for Eve!"')
2.912994146347046
答案 2 :(得分:0)
str.replace("!?'.", ""))
仅替换字符串!?'.
,而不替换四个字符中的任何一个。
您需要为每个字符使用单独的替换调用,否则要使用正则表达式。
答案 3 :(得分:0)
您的解决方案不起作用的原因是,它试图删除文字字符串“!?'。”,而不是单个字符。
一种实现此目的的方法如下:
import re
regex = re.compile('[^a-zA-Z]')
s = "We're having a surprise birthday party for Eve!"
s = regex.sub('', s).lower()
答案 4 :(得分:0)
import re
def preprocess(s):
return re.sub(r'[\W_]', '', s).lower()
re.sub
删除所有非字母数字字符(除A-Z和0-9之外的所有字符)。
lower()
删除大写字母。
答案 5 :(得分:0)
str.translate
也是一个选项。您可以使用str.maketrans
创建一个转换表,其中第一个参数(ascii_uppercase
)将被转换为第二个参数(ascii_lowercase
)。第三个参数(punctuation + whitespace
)是要删除的字符的列表:
from string import ascii_lowercase, ascii_uppercase, punctuation, whitespace
table = str.maketrans(ascii_uppercase, ascii_lowercase, punctuation + whitespace)
s = "We're having a surprise birthday party for Eve!"
print(s.translate(table))
# werehavingasurprisebirthdaypartyforeve
一旦table
初始化后,每个后续字符串都可以通过应用进行转换
s.translate(table)
答案 6 :(得分:0)
不使用RegEx的方法。
>>> import string
>>> s
"We're having a surprise birthday party for Eve!"
>>> s.lower().translate(None, string.punctuation).replace(" ", "")
'werehavingasurprisebirthdaypartyforeve'
答案 7 :(得分:0)
按如下所示更改您的代码:-
def pre_process(s):
str = s.lower()
s = (str.replace(" ", ""))
s= s.replace("!", "")
s= s.replace("'", "")
return s
print(pre_process("We're having a surprise birthday party for Eve!"))