簡體   English   中英

如何獲得元組的所有可能組合(python)

[英]How to get all possible combinations of a tuple (python)

我有一些代碼可以使用元組中的坐標在 3d 空間中繪制一個立方體。 我想通過獲取所有可能的點組合並繪制所有三角形來為立方體添加面。 任何想法如何做到這一點,或者有更好的方法。

有什么幫助,
謝謝!

#the points I use to make the cube
points = [( 1,  1,  1), ( 1, -1,  1), 
          (-1, -1,  1), (-1,  1,  1), 
          ( 1,  1, -1), ( 1, -1, -1), 
          (-1, -1, -1), (-1,  1, -1)]
from itertools import product

def getAllCombos(my_list):
    return list(product(*((x, -x) for x in my_list)))
    
my_list = [1,1,1]

result = getAllCombos(my_list)

print(result)

這將打印: [(1, 1, 1), (1, 1, -1), (1, -1, 1), (1, -1, -1), (-1, 1, 1), (-1, 1, -1), (-1, -1, 1), (-1, -1, -1)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM