Home>

I have two lists

list_1= [['появление', 'NOUN'], ['ООН', 'PROPN'], ['было', 'AUX'], ['обусловлено', 'VERB'], ['целым', 'ADJ'], ['рядом', 'NOUN'], ['объективных', 'ADJ'], ['факторов', 'NOUN'], ['военно', 'ADJ'], ['стратегического', 'ADJ'], ['политического', 'ADJ'], ['экономического', 'ADJ'], ['развития', 'NOUN'], ['человеческого', 'ADJ'], ['общества', 'NOUN'], ['конца', 'NOUN'], ['второго', 'NUM'], ['тысячелетия', 'NOUN'], ['создание', 'NOUN'], ['ООН', 'NOUN'], ['явилось', 'VERB'], ['воплощением', 'NOUN'], ['извечной', 'ADJ'], ['мечты', 'NOUN'], ['человечества', 'NOUN'], ['о', 'ADP'], ['таком', 'PRON'], ['устройстве', 'NOUN'], ['и', 'CCONJ'], ['организации', 'NOUN'], ['международного', 'ADJ'], ['общежития', 'NOUN'], ['которые', 'PRON'], ['избавили', 'VERB'], ['бы', 'x'], ['человечество', 'NOUN'], ['от', 'ADP'], ['бесконечности', 'NOUN'], ['череды', 'NOUN'], ['войн', 'NOUN'], ['и', 'CCONJ'], ['обеспечили', 'VERB'], ['бы', 'x'], ['мирные', 'ADJ'], ['условия', 'NOUN'], ['жизни', 'NOUN'], ['народов', 'NOUN'], ['их', 'PRON'], ['поступательное', 'ADJ']]
list_2= [['появление', 'NOUN'], ['ООН', 'NOUN'], ['было', 'VERB'], ['обусловлено', 'VERB'], ['целым', 'ADJ'], ['рядом', 'NOUN'], ['объективных', 'ADJ'], ['факторов', 'NOUN'], ['военно', 'ADJ'], ['стратегического', 'ADJ'], ['политического', 'ADJ'], ['экономического', 'ADJ'], ['развития', 'NOUN'], ['человеческого', 'ADJ'], ['общества', 'NOUN'], ['конца', 'NOUN'], ['второго', 'NUM'], ['тысячелетия', 'NOUN'], ['создание', 'NOUN'], ['ООН', 'PROPN'], ['явилось', 'VERB'], ['воплощением', 'NOUN'], ['извечной', 'ADJ'], ['мечты', 'NOUN'], ['человечества', 'NOUN'], ['о', 'ADP'], ['таком', 'DET'], ['устройстве', 'NOUN'], ['и', 'CCONJ'], ['организации', 'NOUN'], ['международного', 'ADJ'], ['общежития', 'NOUN'], ['которые', 'PRON'], ['избавили', 'VERB'], ['бы', 'x'], ['человечество', 'NOUN'], ['от', 'ADP'], ['бесконечности', 'NOUN'], ['череды', 'NOUN'], ['войн', 'NOUN'], ['и', 'CCONJ'], ['обеспечили', 'VERB'], ['бы', 'x'], ['мирные', 'ADJ'], ['условия', 'NOUN'], ['жизни', 'NOUN'], ['народов', 'NOUN'], ['их', 'PRON'], ['поступательное', 'ADJ']]

I want to compare two lists and count how many elements match. my code:

result22= []
for c in list_1:
    for x in list_2:
        if c== x:
            result22.append(c)
        else:
            break
print(len(result22))

My answer is 1, but it should be about 45. Can you please tell me where the error is?

  • Answer # 1

    If the first element of the second list is not equal to the current element of the first list, then no further comparison is performed. Need to removeelse...

    if c== x:
        result22.append (c)
        break
    

    There is no need. You only need to remove esle :, because the search should stop. That is, put break after result22.append (c).

    Michael Tetelev2021-09-29 14:07:54

    @MichaelTetelev yes that's right

    Эникейщик2021-09-29 14:08:47

    @MichaelTetelev He gives me 47, that is, for some reason he considers the list ['UN', 'PROPN']. And this happens twice

    irina20202021-09-29 14:17:20

    @ Enikeyshchik He gives me 47, that is, for some reason he considers the list ['UN', 'PROPN']. And this happens twice

    irina20202021-09-29 14:27:04

    @ irina2020 why shouldn't it? It is on both lists, therefore it counts

    Эникейщик2021-09-29 14:28:02
  • Answer # 2

    if setpandasthen such a slightly cumbersome expression will return the desired result:

    import pandas as pd
    print (pd.Series (map ('' .join, list_1)). isin (list (map ('' .join, list_2))). sum ()) # 47
    

    and if so, then repetitions will not be counted:

    print (pd.Series (map ('' .join, list_1)). drop_duplicates ()
         .isin (list (map ('' .join, list_2))). sum ()) # 45
    
  • Answer # 3

    The error lies in the conditionelsewhich is called by the operatorbreak... In this case, at the first inequality of two elements, you simply leave the list. For everything to work, you just need to remove the branchelse...

    Important to notice! In case you have several identical elements in the list, they will be read more times than they are mentioned there