gpt4 book ai didi

python - 从 Python 列表中选择三个不同的值

转载 作者:太空狗 更新时间:2023-10-29 22:07:41 25 4
gpt4 key购买 nike

我有一个点列表及其坐标,如下所示:

[(0,1),(2,3),(7,-1) and so on.]

迭代它们并每次选择三个不同的 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:

所以我寻求帮助。

最佳答案

import random

lst = [(0, 1), (2, 3), (7, -1), (1, 2), (4, 5)]

random.sample(lst, 3)

这只会给你从列表中随机选择的 3 点。看来您可能想要一些不同的东西。你能澄清一下吗?

关于python - 从 Python 列表中选择三个不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34683400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com