生成两个列表的所有组合,并在python中一一输出 [英] Generating all the combinations of two lists and output them one by one in python
问题描述
我有两个列表
[1, 3, 4] [7, 8]
我想从最小组合(例如17,18,37,38,47,48,137,138,147,148......178,378....
)开始生成两个列表的所有组合
现在,对于每个组合,我必须在其他地方测试它的存在,如果我发现该组合存在,那么我将停止组合生成.例如,如果我看到17
存在,那么我将不会生成其他组合.再一次,如果我发现48
存在,那么我将不会生成以后的组合.
I want to generate all the combinations of two list starting from the smallest combinations like 17,18,37,38,47,48,137,138,147,148......178,378....
Now for each combination I have to test it's presence in some other place and if I found that combination to be present then I will stop the combination generation. For example If I see that 17
is present then I will not generate the other combinations. Again If I found 48
to be present then I will not generate the later combinations.
推荐答案
这是一个非常丑陋的算法,但是对我有用.它也不是超级昂贵的(当然,期望使用itertools.combinations(a,i)...生成所有组合):
This is a pretty ugly algorithm, but it worked for me. It's also not super expensive (expect, of course, for generating all the combinations with itertools.combinations(a, i)...):
import itertools
def all_combs(a):
to_return = []
temp = []
for i in a:
temp.append(i)
to_return.append(temp)
for i in range(2, len(a) + 1):
temp = []
for j in itertools.combinations(a, i):
s = ""
for k in j:
s = s + str(k)
temp.append(int(s)) #Get all values from the list permutation
to_return.append(temp)
print(to_return)
return to_return
def all_perm(a, b):
a_combs = all_combs(a)
b_combs = all_combs(b)
to_return = []
for i in a_combs:
for j in b_combs:
for k in i:
for l in j:
to_return.append(10**len(str(l)) * k + l)
to_return.sort()
for i in to_return:
yield i
修复了一个错误,该错误导致无法正确读取多位数的值 使函数充当生成器 修复了涉及数字的错误(通过添加排序...)
Fixed a bug where multi-digit values weren't read in correctly Made the function act as a generator Fixed a bug involving digits (by adding a sort...)
这是一个非常出色的实现,可以更紧密地满足生成器样式.它仍然不是完美的,但是在一般情况下应该可以提供良好的加速效果:
Here's a vastly superior implementation which meets the generator style much more closely. It's still not perfect, but it should provide good speedup in the average case:
import itertools
def add_to_dict(dict, length, num):
if not length in dict:
dict[length] = []
dict[length].append(num)
def sum_to_val(val):
to_return = []
for i in range(1, val):
to_return.append([i, val-i])
return to_return
def all_combs(a):
to_return = {}
for i in a:
add_to_dict(to_return, len(str(i)), i)
for i in range(2, len(a) + 1):
for j in itertools.combinations(a, i):
s = ""
for k in j:
s = s + str(k)
add_to_dict(to_return, len(s), int(s)) #Get all values from the list permutation
return to_return
def all_perm(a, b):
a_combs = all_combs(a)
b_combs = all_combs(b)
for val in range(max(a_combs.keys())+max(b_combs.keys())+1):
to_return = []
sums = sum_to_val(val)
for i in sums:
if not(i[0] in a_combs and i[1] in b_combs):
continue
for j in a_combs[i[0]]:
for k in b_combs[i[1]]:
to_return.append(10**len(str(k)) * j + k)
to_return.sort()
for i in to_return:
yield i
这篇关于生成两个列表的所有组合,并在python中一一输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!