如何找到特定范围内的所有整数集?

我有一个列表列表,我想找到列表中项目的所有排列。我很难解释这一点,所以这里有一个例子。我有三个清单:


level_list = [

    [1, 2, 3],

    [1, 2],

    [1, 2, 3, 4, 5]

]

我想最终得到一个列表列表,所有列表的长度都是 3,并且包含我原来的 3 个列表中的潜在选项;像这样:


final_list =[

    [1, 1, 1],

    [1, 1, 2],

    [1, 1, 3],

    [1, 1, 4],

    [1, 1, 5],

    [1, 2, 1],

    [1, 2, 2],

    [1, 2, 3],

    [1, 2, 4],

    [1, 2, 5],

    [2, 1, 1],

    [2, 1, 2],

    [2, 1, 3],

    [2, 1, 4],

    [2, 1, 5],

    [2, 2, 1],

    [2, 2, 2],

    [2, 2, 3],

    [2, 2, 4],

    [2, 2, 5],

    [3, 1, 1],

    [3, 1, 2],

    [3, 1, 3],

    [3, 1, 4],

    [3, 1, 5],

    [3, 2, 1],

    [3, 2, 2],

    [3, 2, 3],

    [3, 2, 4],

    [3, 2, 5]

]

感觉如果我要手动执行此操作,我应该做我会做的事情,即:

  • 增加最后一个子列表,将其他两个常量的值保持为 1

  • 增加中间子列表,将第一个子列表的值保持为 1 并继续改变最终子列表

  • 完成第一个子列表

如果我对列表的 # 进行硬编码,我可以使用嵌套的 for 循环来做到这一点,但这感觉非常“非 python 风格”,而且也不是真正可行,因为我最终需要它成为一个可以与任意数量的子列表一起使用的函数:

final_list = []

for i1 in level_list[0]:

    for i2 in level_list[1]:

        for i3 in level_list[2]:

            final_list.append([i1, i2, i3])

            

print(final_list)

这几乎就像我需要一个 for 循环嵌套的 for 循环。或者一些比我想象的更聪明的解决方案。我也愿意接受只接受最小值和最大值的解决方案——这将始终是整数列表,它们之间的步长为 1。


慕盖茨4494581
浏览 112回答 2
2回答

一只名叫tom的猫

你可以用itertools.product那个计算输入可迭代对象的笛卡尔积。final_list = list(itertools.product(*level_list)))例子list(itertools.product(*[[1, 2], [3, 4]]))) # [(1, 3), (1, 4), (2, 3), (2, 4)]

蝴蝶刀刀

&nbsp; &nbsp;level_list = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1, 2, 3],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1, 2],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1, 2, 3, 4, 5]&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; nb_lists = len(level_list)&nbsp; &nbsp; &nbsp; &nbsp; least_significant_position = nb_lists - 1&nbsp; &nbsp; &nbsp; &nbsp; indices = defaultdict(int)&nbsp; &nbsp; &nbsp; &nbsp; solution_found = False&nbsp; &nbsp; &nbsp; &nbsp; solution = list()&nbsp; &nbsp; &nbsp; &nbsp; while not solution_found:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; possible_combination = list()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index_incremented = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i in range(nb_lists, 0, -1):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_selection = level_list[i - 1]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index = indices[i - 1]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; possible_combination.insert(0, current_selection[index])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max_index_for_this_list = len(current_selection) - 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not index_incremented and index < max_index_for_this_list:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; indices[i - 1] += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index_incremented = True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if i - 1 != least_significant_position:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; less_significant_position = i&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while less_significant_position <= least_significant_position:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; indices[less_significant_position] = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; less_significant_position += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; solution.append(possible_combination)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not index_incremented:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; solution_found = True&nbsp; &nbsp; &nbsp; &nbsp; print(solution.__str__())
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python