GVKun编程网logo

在python中生成列表的所有组合(在python中生成列表的所有组合)

13

此处将为大家介绍关于在python中生成列表的所有组合的详细内容,并且为您解答有关在python中生成列表的所有组合的相关问题,此外,我们还将为您介绍关于python中生成列表矩阵、Python-如何

此处将为大家介绍关于在python中生成列表的所有组合的详细内容,并且为您解答有关在python中生成列表的所有组合的相关问题,此外,我们还将为您介绍关于python 中生成列表矩阵、Python-如何生成一个列表的所有排列?、Python-清单清单的所有组合、Python-获取列表的所有组合的有用信息。

本文目录一览:

在python中生成列表的所有组合(在python中生成列表的所有组合)

在python中生成列表的所有组合(在python中生成列表的所有组合)

这是问题:

给定Python中的项目列表,我将如何获得这些项目的所有可能组合?

这个站点上有几个类似的问题,建议使用itertools.combine,但是仅返回我需要的一部分:

stuff = [1, 2, 3]for L in range(0, len(stuff)+1):    for subset in itertools.combinations(stuff, L):        print(subset)()(1,)(2,)(3,)(1, 2)(1, 3)(2, 3)(1, 2, 3)

如您所见,它仅按严格顺序返回项目,而不返回(2,1),(3,2),(3,1),(2、1、3),(3、1、2),(
2,3,1)和(3,2,1)。有一些解决方法吗?我似乎什么都没想。

答案1

小编典典

用途itertools.permutations

>>> import itertools>>> stuff = [1, 2, 3]>>> for L in range(0, len(stuff)+1):        for subset in itertools.permutations(stuff, L):                print(subset)...         ()(1,)(2,)(3,)(1, 2)(1, 3)(2, 1)(2, 3)(3, 1)....

帮助itertools.permutations

permutations(iterable[, r]) --> permutations objectReturn successive r-length permutations of elements in the iterable.permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)>>>

python 中生成列表矩阵

python 中生成列表矩阵

 

001、

>>> [[0] * 5 for i in range(3)]                        ## 生成3行5列,元素为0的矩阵
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

 

002、

>>> [ [1, 2, 3] * 2 for i in range(6)]           ## 生成6行6列矩阵, 元素为1、2、3
[[1, 2, 3, 1, 2, 3], [1, 2, 3, 1, 2, 3], [1, 2, 3, 1, 2, 3], [1, 2, 3, 1, 2, 3], [1, 2, 3, 1, 2, 3], [1, 2, 3, 1, 2, 3]]

 

Python-如何生成一个列表的所有排列?

Python-如何生成一个列表的所有排列?

如何解决Python-如何生成一个列表的所有排列??

从Python 2.6(如果你使用的是Python 3)开始,你可以使用标准库工具:itertools.permutations。

import itertools
list(itertools.permutations([1, 2, 3]))

如果你出于某种原因使用旧版Python(<2.6),或者只是想知道它的工作原理,那么这是一种不错的方法,取自 http://code.activestate.com/recipes/252178/:

def all_perms(elements):
    if len(elements) <=1:
        yield elements
    else:
        for perm in all_perms(elements[1:]):
            for i in range(len(elements)):
                # nb elements[0:1] works in both string and list contexts
                yield perm[:i] + elements[0:1] + perm[i:]

的文档中列出了几种替代方法itertools.permutations。这是一个:

def permutations(iterable, r=None):
    # permutations(''ABCD'', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
    # permutations(range(3)) --> 012 021 102 120 201 210
    pool = tuple(iterable)
    n = len(pool)
    r = n if r is None else r
    if r > n:
        return
    indices = range(n)
    cycles = range(n, n-r, -1)
    yield tuple(pool[i] for i in indices[:r])
    while n:
        for i in reversed(range(r)):
            cycles[i] -= 1
            if cycles[i] == 0:
                indices[i:] = indices[i+1:] + indices[i:i+1]
                cycles[i] = n - i
            else:
                j = cycles[i]
                indices[i], indices[-j] = indices[-j], indices[i]
                yield tuple(pool[i] for i in indices[:r])
                break
        else:
            return

另一个基于itertools.product

def permutations(iterable, r=None):
    pool = tuple(iterable)
    n = len(pool)
    r = n if r is None else r
    for indices in product(range(n), repeat=r):
        if len(set(indices)) == r:
            yield tuple(pool[i] for i in indices)

解决方法

如何在Python中生成一个列表的所有排列,独立于该列表中元素的类型?
例如:

permutations([])
[]

permutations([1])
[1]

permutations([1,2])
[1,2]
[2,1]

permutations([1,2,3])
[1,3]
[1,3,1,3]
[2,1]
[3,2]
[3,1]

Python-清单清单的所有组合

Python-清单清单的所有组合

我基本上是在寻找组合的 python版本List<List<int>>

给定一个列表列表,我需要一个新列表,该列表给出列表之间所有可能的项目组合。

[[1,2,3],[4,5,6],[7,8,9,10]] -> [[1,4,7],[1,4,8],...,[3,6,10]]

列表的数量是未知的,因此我需要一些适用于所有情况的列表。奖励积分,尽享优雅!

答案1

小编典典

你需要itertools.product

>>> import itertools>>> a = [[1,2,3],[4,5,6],[7,8,9,10]]>>> list(itertools.product(*a))[(1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 4, 10), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 5, 10), (1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 6, 10), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 4, 10), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 5, 10), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 6, 10), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 4, 10), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 5, 10), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 6, 10)]

Python-获取列表的所有组合

Python-获取列表的所有组合

我知道我可以使用itertools.permutation获得大小为r的所有排列。但是,对于itertools.permutation([1,2,3,4],3)它将返回(1,3)以及(1,2)

  1. 我想过滤这些重复(即获得组合)

  2. 是否有一种简单的方法来获得所有排列(所有长度)?

  3. 如何将itertools.permutation()结果转换为常规列表?

今天的关于在python中生成列表的所有组合在python中生成列表的所有组合的分享已经结束,谢谢您的关注,如果想了解更多关于python 中生成列表矩阵、Python-如何生成一个列表的所有排列?、Python-清单清单的所有组合、Python-获取列表的所有组合的相关知识,请在本站进行查询。

本文标签: