首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >迭代2置换集

迭代2置换集
EN

Stack Overflow用户
提问于 2022-09-01 03:49:19
回答 2查看 56关注 0票数 -2

假设我从这两个假设的列表开始:颜色和形状。

我有15种颜色,假设有一个递归的无限多个形状(就像一个正方形的边数)。在最基本的情况下,它开始于

代码语言:javascript
复制
color_list, shape_list = [1], [1]

就我提到的复杂性而言,长度3的一个例子可能是

代码语言:javascript
复制
color_list, shape_list = [15,9,7], [1,3,7]

这些数字是任意的,以比较将颜色15与形状1、颜色9与形状3、颜色7与形状7相结合的特定排列。

我将如何在每一个可能的排列上创建一个迭代,从[1], [1]返回一些东西,比如[15,15,15,15,15], [n,n,n,n,n]

EN

回答 2

Stack Overflow用户

发布于 2022-09-01 04:17:30

IIUC,你想做的是-

  1. 为每个(颜色= 3,形状= 5),(颜色= 1,形状= 99)创建颜色和形状组合,等等。然后,您将尝试对从1到m(在您的示例中为m=5)的

的每个排列顺序对这些颜色组合进行置换。

下面是一段代码,可以为您提供类似的信息。请注意,这将是成倍的爆炸,因此避免更大的限制。

代码语言:javascript
复制
import itertools

color_i = 1
color_n = 5

shape_i = 1
shape_n = 6

permutes = 3

# color, shape combos
combos = list(itertools.product(range(color_i, color_n+1), range(shape_i, shape_n+1)))

# create permutations from the color, shape combos
permutations = []
for p in range(permutes):
    for i in itertools.permutations(combos,r=p+1):
        permutations.append(list(zip(*i)))

permutations
代码语言:javascript
复制
[[(1,), (1,)],
 [(1,), (2,)],
 [(1,), (3,)],
 [(1,), (4,)],
 [(1,), (5,)],
 [(1,), (6,)],
 [(2,), (1,)],
 [(2,), (2,)],
 [(2,), (3,)],
 [(2,), (4,)],
 [(2,), (5,)],
 [(2,), (6,)],
 [(3,), (1,)],
 [(3,), (2,)],
 [(3,), (3,)],
 [(3,), (4,)],
 [(3,), (5,)],
 [(3,), (6,)],
 [(4,), (1,)],
 [(4,), (2,)],
 [(4,), (3,)],
 [(4,), (4,)],
 [(4,), (5,)],
 [(4,), (6,)],
 [(5,), (1,)],
 [(5,), (2,)],
 [(5,), (3,)],
 [(5,), (4,)],
 [(5,), (5,)],
 [(5,), (6,)],
 [(1, 1), (1, 2)],
 [(1, 1), (1, 3)],
 ...,
 [(1, 4), (1, 2)],
 [(1, 4), (1, 3)],
 [(1, 4), (1, 4)],
 [(1, 4), (1, 5)],
 [(1, 4), (1, 6)],
 [(1, 5), (1, 1)],
 [(1, 5), (1, 2)],
 [(1, 5), (1, 3)],
 [(1, 5), (1, 4)],
 [(1, 5), (1, 5)],
 [(1, 5), (1, 6)],
 [(1, 1), (2, 1)],
 [(1, 1), (2, 3)],
 [(1, 1), (2, 4)],
 [(1, 1), (2, 5)],
 [(1, 1), (2, 6)],
 [(1, 2), (2, 1)],
 [(1, 2), (2, 2)],
 ...,
 [(4, 4), (3, 6)],
 [(4, 5), (3, 1)],
 [(4, 5), (3, 2)],
 [(4, 5), (3, 3)],
 [(4, 5), (3, 4)],
 [(4, 5), (3, 5)],
 [(4, 5), (3, 6)],
 [(4, 1), (4, 1)],
 [(4, 1), (4, 2)],
 [(4, 1), (4, 3)],
 [(4, 1), (4, 4)],
 [(4, 1), (4, 5)],
 ...,
 [(5, 5), (6, 2)],
 [(5, 5), (6, 3)],
 [(5, 5), (6, 4)],
 [(5, 5), (6, 5)],
 [(1, 1, 1), (1, 2, 3)],
 [(1, 1, 1), (1, 2, 4)],
 [(1, 1, 1), (1, 2, 5)],
 [(1, 1, 1), (1, 2, 6)],
 [(1, 1, 2), (1, 2, 1)],
 [(1, 1, 2), (1, 2, 2)],
 [(1, 1, 2), (1, 2, 3)],
 ...,
 [(1, 1, 4), (1, 2, 6)],
 [(1, 1, 5), (1, 2, 1)],
 [(1, 1, 5), (1, 2, 2)],
 [(1, 1, 5), (1, 2, 3)],
 [(1, 1, 5), (1, 2, 4)],
 [(1, 1, 5), (1, 2, 5)],
 [(1, 1, 5), (1, 2, 6)],
 [(1, 1, 1), (1, 3, 2)],
 [(1, 1, 1), (1, 3, 4)],
 [(1, 1, 1), (1, 3, 5)],
 [(1, 1, 1), (1, 3, 6)],
 [(1, 1, 2), (1, 3, 1)],
 [(1, 1, 2), (1, 3, 2)],
 [(1, 1, 2), (1, 3, 3)],
 [(1, 1, 2), (1, 3, 4)],
 [(1, 1, 2), (1, 3, 5)],
 [(1, 1, 2), (1, 3, 6)],
 [(1, 1, 3), (1, 3, 1)],
 [(1, 1, 3), (1, 3, 2)],
 [(1, 1, 3), (1, 3, 3)],
 [(1, 1, 3), (1, 3, 4)],
 [(1, 1, 3), (1, 3, 5)],
 [(1, 1, 3), (1, 3, 6)],
 [(1, 1, 4), (1, 3, 1)],
 [(1, 1, 4), (1, 3, 2)],
 [(1, 1, 4), (1, 3, 3)],
 [(1, 1, 4), (1, 3, 4)],
 [(1, 1, 4), (1, 3, 5)],
 [(1, 1, 4), (1, 3, 6)],
 [(1, 1, 5), (1, 3, 1)],
 [(1, 1, 5), (1, 3, 2)],
 [(1, 1, 5), (1, 3, 3)],
 [(1, 1, 5), (1, 3, 4)],
 [(1, 1, 5), (1, 3, 5)],
 [(1, 1, 5), (1, 3, 6)],
 [(1, 1, 1), (1, 4, 2)],
 [(1, 1, 1), (1, 4, 3)],
 [(1, 1, 1), (1, 4, 5)],
 [(1, 1, 1), (1, 4, 6)],
 [(1, 1, 2), (1, 4, 1)],
 [(1, 1, 2), (1, 4, 2)],
 [(1, 1, 2), (1, 4, 3)],
 [(1, 1, 2), (1, 4, 4)],
 [(1, 1, 2), (1, 4, 5)],
 [(1, 1, 2), (1, 4, 6)],
 [(1, 1, 3), (1, 4, 1)],
 [(1, 1, 3), (1, 4, 2)],
 [(1, 1, 3), (1, 4, 3)],
 [(1, 1, 3), (1, 4, 4)],
 [(1, 1, 3), (1, 4, 5)],
 [(1, 1, 3), (1, 4, 6)],
 ...]

它是如何运作的-

  1. 使用颜色和形状的范围创建itertools.product。这为您提供了所有可能的(颜色、形状)元组。

置换阶为1到m (1,2,3,.,m)的

  1. 在所有可能的元组k倍上置换,其中1>=k>=m

[(color1, color2, color3), (shape1, shape2, shape3)]

  • 在每个生成的置换上使用list(zip(*i))将其从[(color1, shape1), (color2, shape2), (color3, shape3)]转到[(color1, shape1), (color2, shape2), (color3, shape3)]
票数 0
EN

Stack Overflow用户

发布于 2022-09-01 04:17:44

您有许多参数:

  • 可能的颜色数(例如,
  • )可能的形状数(例如,
  • )颜色/形状组合的数目(例如,

)

在Python中:

代码语言:javascript
复制
n_colours = 2
colours = range(1, n_colours+1)

n_shapes = 3
shapes = range(1, n_shapes+1)

# creating a list of all possible combinations of colours and shapes:
all_pairs = list(product(colours, shapes))

您要求在两个不同的颜色和相应形状的列表中获得这些组合。将它们作为成对的元组来生成似乎更明智:

代码语言:javascript
复制
from itertools import product

max_len = 4
tuples = [t for p in [product(all_pairs, repeat=n) for n in range(1, max_len+1)] for t in p]

但是,如果您想要将它们生成成一对独立的颜色和形状,下面是一个实现,它一个接一个地生成它们:

代码语言:javascript
复制
from itertools import product


def all_colour_shape_pairs(n_colours, n_shapes, max_len):
    ps = list(product(range(1, n_colours+1), range(1, n_shapes+1)))
    for t in [t for p in [product(ps, repeat=n) for n in range(1, max_len+1)] for t in p]:
        yield zip(*t)


result = all_colour_shape_pairs(2, 3, 4)
while input('Press enter for another, or enter "x" to stop:') != 'x':
    print(tuple(next(result)))

输出

代码语言:javascript
复制
((1,), (1,))
((1,), (2,))
((1,), (3,))
((2,), (1,))
...
((2,), (3,))
((1, 1), (1, 1))
((1, 1), (1, 2))
...
((2, 2), (3, 3))
((1, 1, 1), (1, 1, 1))
((1, 1, 1), (1, 1, 2))
...

如果希望生成器继续生成“不合理”的结果(或者更确切地说,在计算机耗尽资源并无法继续时停止):

代码语言:javascript
复制
from itertools import product


def all_colour_shape_pairs(n_colours, n_shapes, max_len):
    ps = list(product(range(1, n_colours+1), range(1, n_shapes+1)))
    n = 1
    while True:
        for t in product(ps, repeat=n):
            yield zip(*t)
        n += 1


result = all_colour_shape_pairs(2, 3, 4)
while input('Press enter for another, or enter "x" to stop:') != 'x':
    print(tuple(next(result)))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73564123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档