mlds522
mlds522

Reputation: 65

Compare two bigrams lists and return the matching bigram

Please suggest how to compare 2 bigrams lists and return the matching bigram only. From the below example lists, how to return the matching bigrams ['two', 'three'].

bglist1 = 
[['one', 'two'],
 ['two', 'three'],
 ['three', 'four']]

bglist2 = 
[['one', 'six'],
 ['two', 'four'],
 ['two', 'three']]

Upvotes: 0

Views: 337

Answers (1)

duckboycool
duckboycool

Reputation: 2455

You could just test for if the bigram is in the other list of bigrams.

out = list()

for x in bglist1:
    if x in bglist2:
        out.append(x)

This would give you a list of lists that are in both bglists.

Upvotes: 1

Related Questions