我是Python的新手,我在elif语句中打印“词汇,句法”有问题。结果表明,即使在str中,“词汇”也有词汇和句法两个词。我的elif语句有问题,并且已经尝试了恢复它的方法,但是仍然没有得到正确的结果。
它只打印“词汇”,应该打印“词汇,句法”,因为在str中有‘和’(从句法)和‘参考’(从词法)。
提前感谢您的帮助:)
lexical = [' bound ', ' break ',' call ', ' content ', ' continue ', ' contract ', ' count ', ' direct ', ' even ', ' express ', ' form ', \
' forward ', ' function ', ' get ', ' job ', ' level ', ' name ', ' notice ', ' number ', ' out ', ' part ', ' position ', ' record ', \
' reference ', ' return ', ' set ', ' source ', ' special ', ' standard ', ' string ', ' subject ', ' switch ', ' tail ', ' throw ', \
' translate ', ' try ', ' under ', ' value ', ' way ']
syntactic = [' and ', ' but ', ' or ', ' so ', ' because ', ' however ', ' after ', ' since ', ' during ', ' then ', ' unless ', ' that ', ' while ', \
' also ', ' if ', ' only if ', ' if then ', ' for ', ' just ', ' just as ', ' neither ', ' nor ', ' either ', ' not only ', ' whether ', \
' yet ', ' once ', ' whenever ', ' while ', ' although ', ' not only ', ' but also ', ' before ', ' as much as ', ' as soon as ', \
' as long as ', ' as though ', ' though ']
semantic = [' all ', ' some ', ' more ', ' a lot of ', ' enough ', ' any ', ' most ', ' lots of ', ' less ', ' many ', ' not many ', ' each ', ' a few ', \
' least ', ' several ', ' both ', ' fewer ', ' a little ', ' little ', ' very little ', ' a bit ', ' large amount ', ' large quantity ', ' much ', \
' none ', ' not any ', ' a lot of ', ' plenty of ', ' a number ', ' large number ', ' majority of ', ' great number of ']
pragmatic = [' I ', ' he ', ' she ',' it ', ' me ', ' her ', ' him ', ' them ', ' hers ', ' his ', ' its ', ' your ', ' their ', ' our ', ' herself ', ' himself ', \
' itself ', ' ours ', ' ourselves ', ' yourself ', ' themselves ', ' yourselves ', ' that ', ' theirs ', ' these ', ' they ', ' this ', ' which ', \
' who ', ' you ', ' yours ',' anyone ', ' everyone ', ' somebody ', ' anybody ', ' everybody ', ' something ', ' anything ', ' everything ']
str = "one two three four five and reference "
if any(x in str for x in lexical):
print ("Lexical")
elif any(x in str for x in syntactic):
print ("Syntactic")
elif any(x in str for x in semantic):
print ("Semantic")
elif any(x in str for x in pragmatic):
print ("Pragmatic")
elif any(x in str for x in lexical and syntactic):
print('Lexical, Syntactic')
else:
print ("Clean")发布于 2022-05-20 08:33:17
将只执行if/elif语句链中的第一个匹配项。只有在前面的语句不计算为true时才会检查其他语句(因此,如果是其他语句的话)。在您的示例中,第一个匹配立即发生在第一个语句中,因此您希望它匹配的那个将被忽略。
简单解
简单的解决方案是将这种情况移到顶部,并将您的elif语句从大多数排序到最不具体的位置:
if any(x in str for x in lexical and syntactic):
print('Lexical, Syntactic')
elif any(x in str for x in lexical):
print ("Lexical")
elif any(x in str for x in syntactic):
print ("Syntactic")
elif any(x in str for x in semantic):
print ("Semantic")
elif any(x in str for x in pragmatic):
print ("Pragmatic")
else:
print ("Clean")更好解决方案
虽然这为您的问题产生了正确的结果,但它将很快变得难以维护,因为有许多组合可能。检查每个词汇/句法/语义/语用列表中的单词的简单方法如下:
lexical = [' bound ', ' break ',' call ', ' content ', ' continue ', ' contract ', ' count ', ' direct ', ' even ', ' express ', ' form ', \
' forward ', ' function ', ' get ', ' job ', ' level ', ' name ', ' notice ', ' number ', ' out ', ' part ', ' position ', ' record ', \
' reference ', ' return ', ' set ', ' source ', ' special ', ' standard ', ' string ', ' subject ', ' switch ', ' tail ', ' throw ', \
' translate ', ' try ', ' under ', ' value ', ' way ']
syntactic = [' and ', ' but ', ' or ', ' so ', ' because ', ' however ', ' after ', ' since ', ' during ', ' then ', ' unless ', ' that ', ' while ', \
' also ', ' if ', ' only if ', ' if then ', ' for ', ' just ', ' just as ', ' neither ', ' nor ', ' either ', ' not only ', ' whether ', \
' yet ', ' once ', ' whenever ', ' while ', ' although ', ' not only ', ' but also ', ' before ', ' as much as ', ' as soon as ', \
' as long as ', ' as though ', ' though ']
semantic = [' all ', ' some ', ' more ', ' a lot of ', ' enough ', ' any ', ' most ', ' lots of ', ' less ', ' many ', ' not many ', ' each ', ' a few ', \
' least ', ' several ', ' both ', ' fewer ', ' a little ', ' little ', ' very little ', ' a bit ', ' large amount ', ' large quantity ', ' much ', \
' none ', ' not any ', ' a lot of ', ' plenty of ', ' a number ', ' large number ', ' majority of ', ' great number of ']
pragmatic = [' I ', ' he ', ' she ',' it ', ' me ', ' her ', ' him ', ' them ', ' hers ', ' his ', ' its ', ' your ', ' their ', ' our ', ' herself ', ' himself ', \
' itself ', ' ours ', ' ourselves ', ' yourself ', ' themselves ', ' yourselves ', ' that ', ' theirs ', ' these ', ' they ', ' this ', ' which ', \
' who ', ' you ', ' yours ',' anyone ', ' everyone ', ' somebody ', ' anybody ', ' everybody ', ' something ', ' anything ', ' everything ']
str = "one two three four five and reference "
output_string = ''
if any(x in str for x in lexical):
output_string += "Lexical, "
if any(x in str for x in syntactic):
output_string += "Syntactic, "
if any(x in str for x in semantic):
output_string += "Semantic, "
if any(x in str for x in pragmatic):
output_string += "Pragmatic, "
if output_string == '':
output_string = 'Clean'
print(output_string)在这里,我们创建一个输出字符串,它将包含所有匹配类别,并在每次找到匹配项时附加到该字符串中。注意使用if语句而不是elif语句,以便在找到匹配时检查每一种情况,而不会忽略它们。最后,我们检查输出字符串是否为空(在这种情况下没有找到匹配项),并将输出字符串设置为“清除”。
https://stackoverflow.com/questions/72315118
复制相似问题