[英]Combinations of a list of items in efficient way
I am trying to find if there is a more efficient way of finding these combinations using some Python scientific library.我试图找出是否有更有效的方法来使用某些 Python 科学库来查找这些组合。
I am trying to avoid native for
loops and list append preferring to use some NumPy or similar functionality that in theory should be more efficient given it's using C code under the hood.我试图避免本机for
循环和列表附加更喜欢使用一些 NumPy 或类似的功能,理论上应该更有效,因为它在引擎盖下使用 C 代码。 I am struggling to find one, but to me this is quite a common problem to make these operations in an efficient way rather than using slow Python native structures.我正在努力寻找一个,但对我来说,以一种有效的方式进行这些操作而不是使用缓慢的 Python 本机结构是一个很常见的问题。
I am wondering if I am looking in the wrong places?我想知道我是否在错误的地方寻找? Eg this does not seem to help here: https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.random.binomial.html例如,这在这里似乎没有帮助: https : //docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.random.binomial.html
See here I am taking the binomial coefficients of a list of length 5 starting from a lower bound of 2 and finding out all the possible combinations.请参阅此处,我从 2 的下限开始获取长度为 5 的列表的二项式系数,并找出所有可能的组合。 Meanwhile I append to a global list so I then have a nice list of "taken items" from the original input list.同时,我附加到一个全局列表,这样我就可以从原始输入列表中获得一个很好的“已获取项目”列表。
import itertools
input_list = ['a', 'b', 'c', 'd', 'e']
minimum_amount = 2
comb_list = []
for i in range(minimum_amount, len(input_list)):
curr_list = input_list[:i+1]
print(f"the current index is: {i}, the lists are: {curr_list}")
curr_comb_list = list(itertools.combinations(curr_list, i))
comb_list = comb_list + curr_comb_list
print(f"found {len(comb_list)} combinations (check on set length: {len(set(comb_list))})")
print(comb_list)
Gives:给出:
found 12 combinations (check on set length: 12)
[('a', 'b'), ('a', 'c'), ('b', 'c'), ('a', 'b', 'c'), ('a', 'b', 'd'),
('a', 'c', 'd'), ('b', 'c', 'd'), ('a', 'b', 'c', 'd'), ('a', 'b', 'c', 'e'),
('a', 'b', 'd', 'e'), ('a', 'c', 'd', 'e'), ('b', 'c', 'd', 'e')]
for
loop and using some scientific libraries to do this quicker?是否可以避免for
循环并使用一些科学库来更快地做到这一点?The final list contains all combinations of any length from 1 to len(input_list)
, which is actually the Power Set .最终列表包含从 1 到len(input_list)
的任何长度的所有组合,这实际上是Power Set 。
Look at How to get all possible combinations of a list's elements?查看如何获取列表元素的所有可能组合? . .
You want all combinations from input_list of length 2 or more.您需要长度为2或更多的input_list 中的所有组合。
To get them, you can run:要获取它们,您可以运行:
comb_lst = list(itertools.chain.from_iterable(
[ itertools.combinations(input_list, i)
for i in range(2, len(input_list)) ]))
Something similiar to powerset in examples in the itertools web site, but not exactly the same (the length starts from 2 , not from 1 ).与itertools网站示例中的powerset类似,但不完全相同(长度从2开始,而不是从1 开始)。
Note also that curr_list in your code is actually used only for printing.另请注意,代码中的curr_list实际上仅用于打印。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.