Dmi*_*try 6 python algorithm python-3.x
我有一个带坐标的点列表,如下所示:
[(0,1),(2,3),(7,-1) and so on.]
Run Code Online (Sandbox Code Playgroud)
什么是Pythonic迭代它们并每次选择三种不同的方法?我找不到比使用这样的三个for
循环更简单的解决方案:
for point1 in a:
for point2 in a:
if not point1 == point2:
for point3 in a:
if not point1 == point3 and not point2 == point3:
Run Code Online (Sandbox Code Playgroud)
所以我在寻求帮助.
import random
lst = [(0, 1), (2, 3), (7, -1), (1, 2), (4, 5)]
random.sample(lst, 3)
Run Code Online (Sandbox Code Playgroud)
这将只从列表中随机选择3分.看来你可能想要一些不同的东西.你能澄清一下吗?
你可以使用itertools.combinations
:
from itertools import combinations
for point1, point2, point3 in combinations(points, 3):
...
Run Code Online (Sandbox Code Playgroud)