在 Python 中使用 if else 语句打印 "Lexical, Syntactic"
Print "Lexical, Syntactic" using if else statement in Python
我是 Python 的新手,我在 elif 语句中打印“Lexical, Syntactic”时遇到问题。结果显示“Lexical”,即使在 str 中同时具有 Lexical 和 Syntactic 词。我的 elif 语句有问题,已经尝试了一些方法来恢复它,但仍然没有正确。
它只打印“Lexical”,应该打印“Lexical, Syntactic”因为在 str 中有 'and'(来自语法)和 'reference'(来自词汇)
提前感谢您的帮助:)
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")
只会执行 if/elif
语句链中的第一个匹配项。仅当前面的语句不为真时才会检查其他语句(因此名称为 else if)。在您的情况下,第一个匹配项会在第一个语句中立即发生,因此您期望它匹配的匹配项将被忽略。
简单的解决方案
简单的解决方案是将此案例移至顶部,并将您的 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/syntactic/semantic/pragmatic 列表中单词的简单方法如下:
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
,以便在找到匹配项时检查每个案例而不是忽略它。最后,我们检查输出字符串是否为空(在这种情况下未找到匹配项)并将输出字符串设置为 'Clean'.
我是 Python 的新手,我在 elif 语句中打印“Lexical, Syntactic”时遇到问题。结果显示“Lexical”,即使在 str 中同时具有 Lexical 和 Syntactic 词。我的 elif 语句有问题,已经尝试了一些方法来恢复它,但仍然没有正确。
它只打印“Lexical”,应该打印“Lexical, Syntactic”因为在 str 中有 'and'(来自语法)和 'reference'(来自词汇)
提前感谢您的帮助:)
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")
只会执行 if/elif
语句链中的第一个匹配项。仅当前面的语句不为真时才会检查其他语句(因此名称为 else if)。在您的情况下,第一个匹配项会在第一个语句中立即发生,因此您期望它匹配的匹配项将被忽略。
简单的解决方案
简单的解决方案是将此案例移至顶部,并将您的 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/syntactic/semantic/pragmatic 列表中单词的简单方法如下:
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
,以便在找到匹配项时检查每个案例而不是忽略它。最后,我们检查输出字符串是否为空(在这种情况下未找到匹配项)并将输出字符串设置为 'Clean'.