2015-02-23 24 views
1

我有一系列的維度可以採取離散值。使用itertools或numpy分類的排列

例如,說我有從每一個特定的維度列表包含關鍵字的4個維度:

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

我要迭代並通過這些方面的價值觀的每一個可能的組合做一些東西。

有沒有辦法做到這一點itertoolsnumpy假設兩種不同的情況?

  • 如果每個維度中只能有一個關鍵字
  • 如果每個維度可以有一個或幾個關鍵字

回答

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 
+0

你是什麼意思的'維度'和'number_of_dimensions'? – 2015-02-23 18:05:56

+0

@ user5061這是一種不好的方式來說* iterables。我修正了這個問題。 – 2015-02-23 18:08:53

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') 
... 
  • 如果每個維度中只能有一個關鍵字
  • 如果每個維度可以有一個或幾個關鍵字

這兩種方法將工作,即使你的列表中有每一個元素。

+0

謝謝 - 我猜'itertools.product'有機會比發生器表達式更快(即使我還沒有嘗試過) – Jivan 2015-02-24 10:11:55

+1

@Jivan'itertools.product'在我的測試中比發生器表達式快1.5倍以上,並且是地獄很容易寫和讀。 – 2015-02-24 10:20:48

+0

@ Michael9 ...這是非常感謝:) – Jivan 2015-02-24 10:29:38