简体   繁体   English

制作 arrays 的所有可能组合

[英]Make all possible combinations of arrays

Lets say you have an multidimensional array假设您有一个多维数组

a = np.array([[1, 2], [3, 5], [4,5], [9,5]])

I want all the possible combinations of two arrays given the multidimensional array "a" so that:给定多维数组“a”,我想要两个 arrays 的所有可能组合,以便:

[[1, 2],[3, 5]] , [[1, 2],[4,5]] ... 

I have no idea how to due this.我不知道该怎么做。 Does someone have some suggestions and tips?有人有一些建议和技巧吗?

You can use itertools.combinations function defined in this answer .您可以使用此答案中定义的itertools.combinations function 。 This code creates the list of all the combinations.此代码创建所有组合的列表。

import numpy as np
import itertools

a = np.array([[1, 2], [3, 5], [4,5], [9,5]])
combination=[]  

for L in range(len(a) + 1):
    for subset in itertools.combinations(a, L):
        combination.append([list(sub) for sub in subset])
combination 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM