Python find the frequency of combinations in 2 lists -
i have 2 lists,
list1 = ['a', 'b', 'c', 'a'] list2 = ['a', 'b', 'c', 'd','a']
how can find frequency each combinations of 'a''a'
, 'b''b'
, 'c''c'
?
use aptly named counter
, collections
, this:
>>> collections import counter >>> counter(zip(['a','b','c','a'],['a','b','c','a'])).most_common() [(('a', 'a'), 2), (('b', 'b'), 1), (('c', 'c'), 1)]
zip
creates pairs of objects should compared:
>>> zip(['a','b','c','a'],['a','b','c','a']) [('a', 'a'), ('b', 'b'), ('c', 'c'), ('a', 'a')]
Comments
Post a Comment