我的代码有问题:
from collections import Counter
from collections import defaultdict
from itertools import combinations
def findPairs(pair_counts, n):
pair_counts = dict()
count = Counter(combinations(n, 2))
for key, value in count.items():
pair_counts[key] = value
print(pair_counts)
nums = [2,3,7]
#n = len(nums)
findPairs(pair_counts, nums)
它的输出为:
{(2, 3): 1, (2, 7): 1, (3, 7): 1}
但是我希望它给出的输出看起来更像:
{(2, 3): 1, (2, 7): 1, (3, 7): 1, (3,2):1, (7,2):1, (7,3):1)}
提前致谢
使用置换而不是组合
从itertools导入排列计数=计数器(排列(n,2))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句