多项式和集合
Multidicts and Sets
我有 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])
这有效并输出一个 multidict (from collections import defaultdict),其形式为:
{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}})
我想让三星 returned,但它需要为每个键搜索每组值。因此,如果 Dollar Tree 在第三方公司的价值观中,它也会找到 Dollar Tree。
尝试解决方案:
for key, values in t.items():
for item in values:
if item in values:
print(item)
此外,如果 return 出现 3 次或更多次,是否有办法解决 other_company
? 4次或更多次? m 次或更多次?在 multidict 中。
干杯!
您需要比较作为主词典值的每一对公司集,以便您可以使用 itertools.combinations
来创建这些对,然后检查 set.intersection
与 [=15 的交集=] 交叉点。
for (comp1,comp_set1),(comp2,comp_set2) in combinations(mydict.items(),2) :
print '{} and {}'.fromat(com1,com2),comp_set1.intersections(comp_set2)
使用计数器:
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。
我有 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])
这有效并输出一个 multidict (from collections import defaultdict),其形式为:
{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}})
我想让三星 returned,但它需要为每个键搜索每组值。因此,如果 Dollar Tree 在第三方公司的价值观中,它也会找到 Dollar Tree。
尝试解决方案:
for key, values in t.items():
for item in values:
if item in values:
print(item)
此外,如果 return 出现 3 次或更多次,是否有办法解决 other_company
? 4次或更多次? m 次或更多次?在 multidict 中。
干杯!
您需要比较作为主词典值的每一对公司集,以便您可以使用 itertools.combinations
来创建这些对,然后检查 set.intersection
与 [=15 的交集=] 交叉点。
for (comp1,comp_set1),(comp2,comp_set2) in combinations(mydict.items(),2) :
print '{} and {}'.fromat(com1,com2),comp_set1.intersections(comp_set2)
使用计数器:
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。