同时生成所有可能的组合itertools.combinations_与_替换()与itertools.product()?

2024-10-02 16:31:54 发布

您现在位置:Python中文网/ 问答频道 /正文

在编写一个查找列表所有不同组合的程序时,我发现了很多关于使用intertools.product()而不是{}的线程,就像我一直在做的那样。没有人解释为什么你应该使用intertools.产品. 我想知道这是如何影响我的程序输出。在


Tags: 程序列表产品product线程intertools
1条回答
网友
1楼 · 发布于 2024-10-02 16:31:54

From Python documentation

itertools.product(*iterables[, repeat])

Cartesian product of input iterables.

Equivalent to nested for-loops in a generator expression. For example, product(A, B) returns the same as ((x,y) for x in A for y in B).

换句话说:

for x, y in itertools.product(A, B):

替换

^{pr2}$

编辑:

  • itertolls.COUNDATIONS U与U替换()将使用single iterable并生成其给定长度元素的所有可能组合;

  • OLS.ITERTOR产品()将生成来自多个iterable的值的组合,其中结果元组的元素0来自 第一个iterable,元素1-从第二个开始,等等。

相关问题 更多 >