【问题标题】:How can I find all sets of integers within certain ranges? [duplicate]如何找到特定范围内的所有整数集? [复制]
【发布时间】:2020-09-23 18:51:53
【问题描述】:

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

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 步。

【问题讨论】:

  • 请查看itertools.product()
  • list(itertools.product(*level_list)))
  • 太快了。我想我真的不知道要搜索的正确关键字,所以谢谢大家!

标签: python


【解决方案1】:

你可以使用itertools.product那个

做输入迭代的笛卡尔积。

final_list = list(itertools.product(*level_list)))

例子

list(itertools.product(*[[1, 2], [3, 4]]))) # [(1, 3), (1, 4), (2, 3), (2, 4)]

【讨论】:

    【解决方案2】:

    编辑:@azro 发布的解决方案更好。

            level_list = [
                [1, 2, 3],
                [1, 2],
                [1, 2, 3, 4, 5]
            ]
    
            nb_lists = len(level_list)
            least_significant_position = nb_lists - 1
            indices = defaultdict(int)
            solution_found = False
            solution = list()
    
            while not solution_found:
                possible_combination = list()
                index_incremented = False
    
                for i in range(nb_lists, 0, -1):
                    current_selection = level_list[i - 1]
                    index = indices[i - 1]
                    possible_combination.insert(0, current_selection[index])
    
                    max_index_for_this_list = len(current_selection) - 1
    
                    if not index_incremented and index < max_index_for_this_list:
                        indices[i - 1] += 1
                        index_incremented = True
    
                        if i - 1 != least_significant_position:
                            less_significant_position = i
    
                            while less_significant_position <= least_significant_position:
                                indices[less_significant_position] = 0
                                less_significant_position += 1
    
                solution.append(possible_combination)
    
                if not index_incremented:
                    solution_found = True
    
            print(solution.__str__())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多