使用itertools或numpy进行分类排列

时间:2015-02-23 17:44:12

标签: python python-3.x numpy itertools

我有一系列可以采用离散值的维度。

例如,假设我有4个维度,每个维度都包含特定维度列表中的关键字:

color: black, blue, red, green, yellow
size: xs, s, m, l, xl
material: leather, fabric, cotton, wool
gender: male, female

我希望迭代并通过这些维度值的每种可能组合来做一些事情。

假设有两种不同的情况,是否可以通过itertoolsnumpy执行此操作?

  • 如果每个维度只能有一个关键字
  • 如果每个维度都有一个或多个关键字

2 个答案:

答案 0 :(得分:3)

你试过itertools.product(*iterables)吗?听起来像是你正在寻找的东西。

该函数可以根据需要使用尽可能多的迭代,并生成笛卡尔积。这是一个例子:

import itertools

dimension1 = range(3)
dimension2 = ['a']
dimension3 = ['hello', 'world']

for res in itertools.product(dimension1, dimension2, dimension3):
    print(*res)

输出:

0 a hello
0 a world
1 a hello
1 a world
2 a hello
2 a world

答案 1 :(得分:1)

  

假设有两种不同的情况,有没有办法用itertools或numpy做到这一点?

使用itertools.product

from itertools import product
gender = ['male', 'female']
color = ['black', 'blue', 'red', 'green', 'yellow']
material = ['leather', 'fabric', 'cotton', 'wool']
size = ['xs', 's','m', 'l', 'xl']
for item in product(color, size, material, gender):
    #do something()

您还可以使用generator expressions

for item in ((g, c, m, s) for g in gender for c in color for m in material for s in size):
     #do something()

输出

>>> for item in ((g, c, m, s) for g in gender for c in color for m in material for s in size):
...     print(item)
... 
('male', 'black', 'leather', 'xs')
('male', 'black', 'leather', 's')
('male', 'black', 'leather', 'm')
('male', 'black', 'leather', 'l')
('male', 'black', 'leather', 'xl')
('male', 'black', 'fabric', 'xs')
('male', 'black', 'fabric', 's')
('male', 'black', 'fabric', 'm')
('male', 'black', 'fabric', 'l')
('male', 'black', 'fabric', 'xl')
('male', 'black', 'cotton', 'xs')
('male', 'black', 'cotton', 's')
('male', 'black', 'cotton', 'm')
('male', 'black', 'cotton', 'l')
('male', 'black', 'cotton', 'xl')
('male', 'black', 'wool', 'xs')
('male', 'black', 'wool', 's')
('male', 'black', 'wool', 'm')
('male', 'black', 'wool', 'l')
('male', 'black', 'wool', 'xl')
('male', 'blue', 'leather', 'xs')
('male', 'blue', 'leather', 's')
('male', 'blue', 'leather', 'm')
('male', 'blue', 'leather', 'l')
('male', 'blue', 'leather', 'xl')
('male', 'blue', 'fabric', 'xs')
('male', 'blue', 'fabric', 's')
('male', 'blue', 'fabric', 'm')
('male', 'blue', 'fabric', 'l')
('male', 'blue', 'fabric', 'xl')
('male', 'blue', 'cotton', 'xs')
('male', 'blue', 'cotton', 's')
('male', 'blue', 'cotton', 'm')
('male', 'blue', 'cotton', 'l')
('male', 'blue', 'cotton', 'xl')
('male', 'blue', 'wool', 'xs')
('male', 'blue', 'wool', 's')
('male', 'blue', 'wool', 'm')
('male', 'blue', 'wool', 'l')
('male', 'blue', 'wool', 'xl')
('male', 'red', 'leather', 'xs')
('male', 'red', 'leather', 's')
('male', 'red', 'leather', 'm')
('male', 'red', 'leather', 'l')
('male', 'red', 'leather', 'xl')
('male', 'red', 'fabric', 'xs')
('male', 'red', 'fabric', 's')
('male', 'red', 'fabric', 'm')
('male', 'red', 'fabric', 'l')
('male', 'red', 'fabric', 'xl')
('male', 'red', 'cotton', 'xs')
('male', 'red', 'cotton', 's')
('male', 'red', 'cotton', 'm')
('male', 'red', 'cotton', 'l')
('male', 'red', 'cotton', 'xl')
('male', 'red', 'wool', 'xs')
('male', 'red', 'wool', 's')
('male', 'red', 'wool', 'm')
('male', 'red', 'wool', 'l')
...
  
      
  • 如果每个维度只能有一个关键字
  •   
  • 如果每个维度都有一个或多个关键字
  •   

即使您的列表每个都有一个元素,这两种方法都会起作用。