we basically want to compare two lists if the same word-combinations occur. Our Trigram-Code brought us something like this:
e.g. (these are type "tuple")
List1 =
(('I', 'want', 'this'),456)
(('What', 'is', 'this') , 25)
List2 =
(('this', 'is', 'what'), 12)#this one should not count, because the order is different
(('I', 'want', 'this'), 9)
The numbers behind each list show how often these trigram-combinations occured in our DataFrame, maybe you have to delete them first?
List3 = Trigram-Word-Combinations that occur in List 1 AND List 2
Result should be "'I', 'want', 'this'"
Thank you in advance
CodePudding user response:
You can use set intersection and only use the tuple of words:
>>> {x[0] for x in List1} & {x[0] for x in List2}
{('I', 'want', 'this')}
CodePudding user response:
List3 = [ word for word in List1[0] if word in List2[0] ]
That should work, it is a list comprehension which is a shorthand way of interating through a list and each time adding (in this case conditionally adding) a related element (in this case the same element) to another list. The traditional way to write this operation would be using a for
loop.
List1 and List2 are not actually lists, they are tuples, a list has square brackets instead of normal brackets, but both are very similar.
List3 = tuple([ word for word in List1[0] if word in List2[0] ])
will return a tuple.
The below answer from a_guest is neater and probably better