简体   繁体   English

从组合列表中获取唯一组

[英]Get the unique groups from a list of combinations

combinations=[[0, 1, 2],[0, 2, 3], [0, 2, 4],[5, 6, 8], [5, 6, 9],[6, 7, 10], [6, 8, 9],[3,4,11],[7,9,10],[1,4,7],[9,10,11]]

output needed=
1) [[0,1,2],[5, 6, 8],[3,4,11],[7,9,10]],
2) [[0, 2, 3],[1,4,7],[5, 6, 8],[9,10,11]]

So I have gotten a list of all possible combination in sets of three, and I wanted to know if I could get a list of combinations in which all elements are there.所以我得到了一个所有可能组合的列表,以三个为一组,我想知道我是否可以获得所有元素都在那里的组合列表。 ie [0,1,2,3,4,5,6,7,8,9,10,11] but in set of 3 Thanks in advance即 [0,1,2,3,4,5,6,7,8,9,10,11] 但在一组 3 提前致谢

This combination is a mathematical techniques with calculates the number of possible arrangements in a collection of list or items.这种组合是一种数学技术,用于计算列表或项目集合中可能的排列数量。 The unique combination of two lists in python can be performed by pairing each element of the first list with the elements of the second list. python中两个列表的唯一组合可以通过将第一个列表的每个元素与第二个列表的元素配对来执行。
It can be done by using the methods namely permutation() of itertools package and zip() function and the other method is by using product() of itertools and zip() function.它可以通过使用 itertools 包的permutation()zip()函数的方法来完成,另一种方法是使用 itertools 的product()zip()函数。

Here is the example:这是示例:

import itertools
from itertools import product

list_1 = ["b","c","d"]
list_2 = [1,4,9]

unique_combinations = []

unique_combinations = list(list(zip(list_1, element))
                           for element in product(list_2, repeat = len(list_1)))

If you are using only numeric data types in your array, you might be better off using numpy .如果您在数组中仅使用数字数据类型,则最好使用numpy

From what I can glean from your question, you need an array containing all the unique elements in combinations , and group them in threes in any random fashion.根据我从您的问题中收集到的信息,您需要一个包含combinations中所有唯一元素的数组,并以任何随机方式将它们分成三组。

import numpy as np

# Extracting the unique elements.
combinations = np.unique(combinations)

# Shuffling the elements.
combinations = np.random.permutation(combinations)

# Reshaping the unique elements in groups of three.
combinations = combinations.reshape((-1, 3))

Refer: NumPy docs参考: NumPy 文档

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

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