我有ExpandToTyped
csv,我创建了一个multidict:
n
这可以使用以下形式工作并输出multidict(来自collections import defaultdict):
for name in filenames:
with open(path+name) as openFile:
reader = csv.reader(openFile)
for line in reader:
if line[1] in t:
pass
elif line[1] == 'filer_name':
pass
else:
t[name[:-8]].add(line[1])
有n家公司有n套其他公司。所以现在,我想对每个键中的other_company说,检查other_company是否在另一个公司的值中。例如:
{company name: {other_company_1, other_company_2,...}}
我希望返回三星,但它需要为每个键搜索每组值。因此,如果Dollar Tree属于第三家公司的价值,它也会找到Dollar Tree。
尝试解决方案:
defaultdict(<class 'set'>, {Apple : {Samsung, Qualcomm, NVidia}},{Microsoft: {Samsung, Alcoa, Dollar Tree}})
此外,如果for key, values in t.items():
for item in values:
if item in values:
print(item)
出现3次或更多次,是否有办法返回?{1}}? 4次以上? m次以上?在多指中。
干杯!
答案 0 :(得分:0)
您需要比较作为主词典值的每对公司集,以便您可以使用itertools.combinations
来创建这些对,然后检查与set.intersection
的交叉点以返回交叉点。
for (comp1,comp_set1),(comp2,comp_set2) in combinations(mydict.items(),2) :
print '{} and {}'.fromat(com1,com2),comp_set1.intersections(comp_set2)
答案 1 :(得分:0)
使用计数器:
from collections import Counter
cnt = Counter()
for key, values in t.items():
for item in values:
cnt[item] += 1
print([comp for comp in cnt if cnt[comp] > 1])
如果您想要N次出现,可以将1更改为2,3。