Python 使用“避免重复代码行”;或;“操作员”;不会给出与复制行时相同的结果

Python 使用“避免重复代码行”;或;“操作员”;不会给出与复制行时相同的结果,python,python-3.x,list,if-statement,tuples,Python,Python 3.x,List,If Statement,Tuples,我有三张不同单词的单子。一个单词可以在多个列表中。我还有一个句子列表。我所做的是:我检查我句子中某个特定索引的单词是否出现在上面的单词列表中,如果“是”,那么我会附加这个句子。我用了两种方法 第一种方法是: 我为一个单词列表编写代码,并复制它,但只更改列表的名称和句子附加的变量 if (pattern_list_tup[pat][2]): if (span[value_of_attribute[pat]] in lexique_1[pattern_list

我有三张不同单词的单子。一个单词可以在多个列表中。我还有一个句子列表。我所做的是:我检查我句子中某个特定索引的单词是否出现在上面的单词列表中,如果“是”,那么我会附加这个句子。我用了两种方法

第一种方法是:

我为一个单词列表编写代码,并复制它,但只更改列表的名称和句子附加的变量

if (pattern_list_tup[pat][2]):
                    if (span[value_of_attribute[pat]] in lexique_1[pattern_list_tup[pat][1]]):
                        if sent not in sentence_extract_lexique_1:
                             sentence_extract_lexique_1.append(sent)                            
                    else:
                        sentence_not_extract_lexique_1.append(sent)

if (pattern_list_tup[pat][2]):
                    if (span[value_of_attribute[pat]] in lexique_2[pattern_list_tup[pat][1]]):
                        if sent not in sentence_extract_lexique_1:
                             sentence_extract_lexique_2.append(sent)                            
                    else:
                        sentence_not_extract_lexique_2.append(sent)

if (pattern_list_tup[pat][2]):
                    if (span[value_of_attribute[pat]] in lexique_3[pattern_list_tup[pat][1]]):
                        if sent not in sentence_extract_lexique_1:
                             sentence_extract_lexique_3.append(sent)                            
                    else:
                        sentence_not_extract_lexique_3.append(sent)
如您所见,我复制了第一行代码,并将其用于其他列表(lexique)。我所做的是我决定打印“三人联盟名单”

详情如下:

Lists = [sentence_extract_lexique_1, sentence_extract_lexique_2, sentence_extract_lexique_3]
    
    all_union = set.union(*map(set, Lists))
    print("Union des 4 dictionnaires pour phrases extraites", len(all_union), "\n")

# Union des 4 dictionnaires pour phrases extraites  1003     
然后,为了不重复第一行,我想出了另一种方法:我使用运算符“or”,如下所示:

 if (pattern_list_tup[pat][2]):
                    if (span[value_of_attribute[pat]] in lexique_1[pattern_list_tup[pat][1]] or lexique_2[pattern_list_tup[pat][1]] or lexique_3[pattern_list_tup[pat][1]] :
                        #print(span[value_of_attribute[pat]])
                        if sent not in sentence:
                            sentence.append(sent)                     
                    else:
                        sentence_not.append(sent)
在这行代码之后,我决定打印结果,但在打印之前,我使用“set”来抑制doublons

print(" phrases totales ", len(sentence))

# phrases totale 1996

sentence = set(sentence)
print("Total après suppression ", len(sentence))

Total après suppression 1556

我想知道为什么结果是不同的,使用“或”,然后删除doublons应该得到与第一种方法相同的结果(我相信)。也许有人能帮我找出原因;我使用第二个来展示我的作品,但之后我重新检查代码并改进,我使用“或”;如果两个答案不相同,这是否意味着我的第一个解决方案是错误的,还是第二个解决方案是正确的。

您不能以这种方式使用“或”:

if (span[value_of_attribute[pat]] in lexique_1[pattern_list_tup[pat][1]] or lexique_2[pattern_list_tup[pat][1]] or lexique_3[pattern_list_tup[pat][1]] :
你要么做

if a in b or a in c....
或者你可以

for _list in [b, c]:
    if a in _list:
        ....
第三种方法是将所有这些代码包装在一个函数中,并使用3个列表中的每一个调用该函数

顺便说一句,doublon在英语中被翻译成副本