月关宝盒
您可以使用collections.defaultdictwithfrozenset和itertools.combinations来形成成对计数的字典。变化是可能的。例如,您可以使用collections.Counterwith sortedtuple来代替,但基本上是相同的想法。from collections import defaultdictfrom itertools import combinationsdd = defaultdict(int)L1 = ["cat", "toe", "man"]L2 = ["cat", "toe", "ice"]L3 = ["cat", "hat", "bed"]for L in [L1, L2, L3]: for pair in map(frozenset, (combinations(L, 2))): dd[pair] += 1结果:defaultdict(int, {frozenset({'cat', 'toe'}): 2, frozenset({'cat', 'man'}): 1, frozenset({'man', 'toe'}): 1, frozenset({'cat', 'ice'}): 1, frozenset({'ice', 'toe'}): 1, frozenset({'cat', 'hat'}): 1, frozenset({'bed', 'cat'}): 1, frozenset({'bed', 'hat'}): 1})