string="I can't wait for Christmas!!"
other_symbols='''!()-[]{};:\<>/?#$%^&*_~'''
if other_symbols in string:
print('wrong')
else:
print('right')
它应该忽略除@
以外的所有符号,并且我的输出应该是错误的,但我一直在正确。检查一个句子是否包含符号
string="I can't wait for Christmas!!"
other_symbols='''!()-[]{};:\<>/?#$%^&*_~'''
if other_symbols in string:
print('wrong')
else:
print('right')
它应该忽略除@
以外的所有符号,并且我的输出应该是错误的,但我一直在正确。检查一个句子是否包含符号
使用any
来检查other_symbols
中的任何符号是否在cavs
中。最好让cavs
一套太为O(1)
成员测试:
sc = set(cavs)
if any(i in sc for i in other_symbols):
print("wrong")
else:
print("right")
或者,如@Steven Rumbalski在评论中指出,你可以扭转这种局面,使other_symbols
一套,做:
if any(c in other_symbols for c in cavs):
# rest similar
这将是如果有多个句子进行测试,这是一个改进,因为您不必为每个句子创建一个集合。
谢谢,但我不能使用任何或设置在我的代码,因为我们从来没有在我的课上了解它的任何东西 – CAVS
然后只是不要把它包装在'set' @CAVS,只是做'任何(我在cavs为我other_symbols)'。 :) –
'如果有(符号在其他_符号中的符号)'。您的支票会检查是否是''''!() - [] {};:\ <> /?#$%^&* _〜''''是''LET'S GO !! @cavs''的子字符串显然是错误的。 –