Python 如何重新编程不显示重复组合?

Python 如何重新编程不显示重复组合?,python,python-3.x,Python,Python 3.x,我在21中找到了可以满足a*a+b*b==c*c的组合数,但我不想重复组合结果。那么如何重新编程,不显示重复的组合号呢 nums = range(1, 21) trips = [(a, b, c) for a in nums for b in nums for c in nums if a*a + b*b == c*c] print(trips) 一个简单的解决方案是使用集合和排序来丢弃重复的元素: nums = range(1, 21) trips = sorted({(tuple(sort

我在21中找到了可以满足
a*a+b*b==c*c
的组合数,但我不想重复组合结果。那么如何重新编程,不显示重复的组合号呢

nums = range(1, 21)
trips = [(a, b, c) for a in nums for b in nums for c in nums if a*a + b*b == c*c]
print(trips)

一个简单的解决方案是使用集合和排序来丢弃重复的元素:

nums = range(1, 21)
trips = sorted({(tuple(sorted((a, b))), c) for a in nums for b in nums for c in nums if a*a + b*b == c*c})
print(trips)
# [((3, 4), 5), ((5, 12), 13), ((6, 8), 10), ((8, 15), 17), ((9, 12), 15), ((12, 16), 20)]
如果要展平元组,请执行以下操作:

trips = [(a, b, c) for (a, b), c in trips]
然而,明智的做法是不要迭代
a
b
的重复组合。您可以这样做:

nums = range(1, 21)
trips = [(a, b, c) for a in nums for b in nums[a:] for c in nums if a*a + b*b == c*c]
print(trips)
# [(3, 4, 5), (5, 12, 13), (6, 8, 10), (8, 15, 17), (9, 12, 15), (12, 16, 20)]
或使用itertools:

from itertools import combinations

nums = range(1, 21)
trips = [(a, b, c) for a, b in combinations(nums, 2) for c in nums if a*a + b*b == c*c]
print(trips)
# [(3, 4, 5), (5, 12, 13), (6, 8, 10), (8, 15, 17), (9, 12, 15), (12, 16, 20)]

这实际上也很简单。

一个简单的解决方案是使用集合和排序来丢弃重复的元素:

nums = range(1, 21)
trips = sorted({(tuple(sorted((a, b))), c) for a in nums for b in nums for c in nums if a*a + b*b == c*c})
print(trips)
# [((3, 4), 5), ((5, 12), 13), ((6, 8), 10), ((8, 15), 17), ((9, 12), 15), ((12, 16), 20)]
如果要展平元组,请执行以下操作:

trips = [(a, b, c) for (a, b), c in trips]
然而,明智的做法是不要迭代
a
b
的重复组合。您可以这样做:

nums = range(1, 21)
trips = [(a, b, c) for a in nums for b in nums[a:] for c in nums if a*a + b*b == c*c]
print(trips)
# [(3, 4, 5), (5, 12, 13), (6, 8, 10), (8, 15, 17), (9, 12, 15), (12, 16, 20)]
或使用itertools:

from itertools import combinations

nums = range(1, 21)
trips = [(a, b, c) for a, b in combinations(nums, 2) for c in nums if a*a + b*b == c*c]
print(trips)
# [(3, 4, 5), (5, 12, 13), (6, 8, 10), (8, 15, 17), (9, 12, 15), (12, 16, 20)]

这实际上也很简单。

我建议您使用计数器,然后按照您的意愿打印没有重复的项目

这是一个开始:

import collections

nums = range(1, 21)
trips = [(a, b, c) for a in nums for b in nums for c in nums if a*a + b*b == c*c]
counted_trips = collections.Counter(trips)
print(counted_trips)

# Then you can only print trips that have occurrency == 1 (unique)
for trip, occurrency in counted_trips.items():
    if occurrency == 1:
        print(trip)
输出:

Counter({(3, 4, 5): 1, (8, 6, 10): 1, (15, 8, 17): 1, (12, 5, 13): 1, (5, 12, 13): 1, (12, 16, 20): 1, (16, 12, 20): 1, (9, 12, 15): 1, (12, 9, 15): 1, (4, 3, 5): 1, (6, 8, 10): 1, (8, 15, 17): 1})

(8, 6, 10)
(15, 8, 17)
(9, 12, 15)
(16, 12, 20)
(12, 9, 15)
(8, 15, 17)
(12, 16, 20)
(4, 3, 5)
(3, 4, 5)
(12, 5, 13)
(6, 8, 10)
(5, 12, 13)

我建议您使用计数器,然后只打印您喜欢的没有重复项的项目

这是一个开始:

import collections

nums = range(1, 21)
trips = [(a, b, c) for a in nums for b in nums for c in nums if a*a + b*b == c*c]
counted_trips = collections.Counter(trips)
print(counted_trips)

# Then you can only print trips that have occurrency == 1 (unique)
for trip, occurrency in counted_trips.items():
    if occurrency == 1:
        print(trip)
输出:

Counter({(3, 4, 5): 1, (8, 6, 10): 1, (15, 8, 17): 1, (12, 5, 13): 1, (5, 12, 13): 1, (12, 16, 20): 1, (16, 12, 20): 1, (9, 12, 15): 1, (12, 9, 15): 1, (4, 3, 5): 1, (6, 8, 10): 1, (8, 15, 17): 1})

(8, 6, 10)
(15, 8, 17)
(9, 12, 15)
(16, 12, 20)
(12, 9, 15)
(8, 15, 17)
(12, 16, 20)
(4, 3, 5)
(3, 4, 5)
(12, 5, 13)
(6, 8, 10)
(5, 12, 13)

更长的代码,但效率更高:我们生成
A
b
的组合,不重复,并使用dict检查
A**2+b**2
是否是我们要查找的值之一,这比在列表中检查要快

from itertools import combinations

maxvalue = 21

roots = {x**2:x for x in range(1, maxvalue)}

solutions = []
for a, b in combinations(range(1, maxvalue), 2):
    c2 = a**2 + b**2
    if c2 in roots:
        solutions.append((a, b, roots[c2]))

print(solutions)

# [(3, 4, 5), (5, 12, 13), (6, 8, 10), (8, 15, 17), (9, 12, 15), (12, 16, 20)]

更长的代码,但效率更高:我们生成
A
b
的组合,不重复,并使用dict检查
A**2+b**2
是否是我们要查找的值之一,这比在列表中检查要快

from itertools import combinations

maxvalue = 21

roots = {x**2:x for x in range(1, maxvalue)}

solutions = []
for a, b in combinations(range(1, maxvalue), 2):
    c2 = a**2 + b**2
    if c2 in roots:
        solutions.append((a, b, roots[c2]))

print(solutions)

# [(3, 4, 5), (5, 12, 13), (6, 8, 10), (8, 15, 17), (9, 12, 15), (12, 16, 20)]

只需将
a
b
c
有序,
您可以添加
a只需按顺序添加
a
b
c
, 您可以添加
a