代码之家  ›  专栏  ›  技术社区  ›  Green Moshe

Python:如何从中的列表中获取所有有序选项

  •  0
  • Green Moshe  · 技术社区  · 7 年前

    我有以下方法

    a = [1, 11, 111]
    b = [2, 22, 222]
    c = [3, 33, 333]
    list_of_lists = [a, b, c]
    lists_with_the_i_elements = [0 for x in range(len(list_of_lists))]
    for i in range(0, len(list_of_lists)):
        lists_with_the_i_elements[i] = [list_i[i] for list_i in list_of_lists]
    result = list(itertools.product(lists_with_the_i_elements[0],lists_with_the_i_elements[1],lists_with_the_i_elements[2]))
    print(result)
    

    雷乌斯尔特:

    [(1, 11, 111),
    (1, 11, 222), 
    (1, 11, 333), 
    (1, 22, 111), 
    (1, 22, 222), 
    (1, 22, 333), 
    (1, 33, 111), 
    (1, 33, 222), 
    (1, 33, 333), 
    (2, 11, 111), 
    (2, 11, 222), 
    (2, 11, 333),
    (2, 22, 111), 
    (2, 22, 222), 
    (2, 22, 333), 
    (2, 33, 111), 
    (2, 33, 222), 
    (2, 33, 333),
    (3, 11, 111),
    (3, 11, 222),
    (3, 11, 333),
    (3, 22, 111), 
    (3, 22, 222), 
    (3, 22, 333), 
    (3, 33, 111), 
    (3, 33, 222), 
    (3, 33, 333)]
    

    [1,2,3]
    [1,22,3]
    [1,222,3]
    [1,2,33]
    [1,22,33]
    [1,222,33]
    [1,2,333]
    [1,22,333]
    [1,222,333]
    [11,2,3]
    [11,22,3]
    [11,222,3]
    [11,2,33]
    [11,22,33]
    [11,222,33]
    [11,2,333]
    [11,22,333]
    [11,222,333]
    ...
    

    我想有一个函数,当它将收到 list_of_lists 它将返回以下输出:

    另一个简单的例子:

    def combo(*args):
         #do something
    ...
    combo([1],[2],[3])
    ===>[1,2,3]
    combo([1],[2],[3,33])
    ===>[1,2,3],[1,2,33]
    

    我把所有的选择都看了一遍 itertools

    1 回复  |  直到 7 年前
        1
  •  3
  •   Fred    7 年前

    类似的内容应该可以帮助您:

    list(itertools.product([1,11,111], [2, 22, 222], [3, 33, 333]))
    
    推荐文章