python - Multidicts and Sets -
i have n
csv's created multidict with:
for name in filenames: open(path+name) openfile: reader = csv.reader(openfile) line in reader: if line[1] in t: pass elif line[1] == 'filer_name': pass else: t[name[:-8]].add(line[1])
this works , outputs multidict (from collections import defaultdict) form:
{company name: {other_company_1, other_company_2,...}}
there n companies n sets of other companies. now, want other_company in each key, check if other_company in values of company. example:
defaultdict(<class 'set'>, {apple : {samsung, qualcomm, nvidia}},{microsoft: {samsung, alcoa, dollar tree}})
i want samsung returned, needs search each set of values every key. if dollar tree in values of third company, find dollar tree too.
attempt @ solution:
for key, values in t.items(): item in values: if item in values: print(item)
additionally, there way return other_company
if occurs 3 or more times? 4 or more times? m or more times? in multidict.
cheers!
use counter:
from collections import counter cnt = counter() key, values in t.items(): item in values: cnt[item] += 1 print([comp comp in cnt if cnt[comp] > 1])
you can change 1 2,3 if want n occurrences.
Comments
Post a Comment