Equivalent function of %in%(from R) for python -


i have 2 columns containing splited sentence col("i" "love" "food") , col2 ("love","food).i want count matched words row wise.like here 2 . want in python.as in r use %in% function that.??

make sets of word lists:

set1 = set(["i", "love", "food"]) set2 = set(["love", "food"]) 

and count number of elements in intersection of set1 , set2:

num_matched = len(set1.intersection(set2))  # returns 2 

note not count multiple matches of same word, , in fact multiples of same word not show in set1 or set2. furthermore, sets not guarantee order elements. helps though.


Comments