【发布时间】:2015-07-23 15:39:42
【问题描述】:
我有 n csv,我用它创建了一个 multidict:
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])
这可以工作并输出一个多字典(从集合导入默认字典),格式如下:
{company name: {other_company_1, other_company_2,...}}
有 n 家公司和 n 组其他公司。所以现在,我想说对于每个键中的 other_company,检查 other_company 是否在另一家公司的值中。示例:
defaultdict(<class 'set'>, {Apple : {Samsung, Qualcomm, NVidia}},{Microsoft: {Samsung, Alcoa, Dollar Tree}})
我希望返回三星,但它需要为每个键搜索每组值。因此,如果 Dollar Tree 在第三家公司的价值观中,它也会找到 Dollar Tree。
尝试解决方案:
for key, values in t.items():
for item in values:
if item in values:
print(item)
此外,如果other_company 出现 3 次或更多次,有没有办法返回它? 4次或更多?米或更多次?在multidict中。
干杯!
【问题讨论】:
标签: python python-3.x dictionary set