R の expand.grid() 関数に似た Python 関数はありますか? 前もって感謝します。
(編集) 以下は、この R 関数の説明と例です。
Create a Data Frame from All Combinations of Factors
Description:
Create a data frame from all combinations of the supplied vectors
or factors.
> x <- 1:3
> y <- 1:3
> expand.grid(x,y)
Var1 Var2
1 1 1
2 2 1
3 3 1
4 1 2
5 2 2
6 3 2
7 1 3
8 2 3
9 3 3
(EDIT2) 以下は rpy パッケージの例です。R を使用せずに同じ出力オブジェクトを取得したい:
>>> from rpy import *
>>> a = [1,2,3]
>>> b = [5,7,9]
>>> r.assign("a",a)
[1, 2, 3]
>>> r.assign("b",b)
[5, 7, 9]
>>> r("expand.grid(a,b)")
{'Var1': [1, 2, 3, 1, 2, 3, 1, 2, 3], 'Var2': [5, 5, 5, 7, 7, 7, 9, 9, 9]}
編集 02/09/2012: Python で本当に迷っています。彼の答えで与えられた Lev Levitsky のコードは私にとってはうまくいきません:
>>> a = [1,2,3]
>>> b = [5,7,9]
>>> expandgrid(a, b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in expandgrid
NameError: global name 'itertools' is not defined
ただし、itertools モジュールはインストールされているようです (入力from itertools import *
してもエラー メッセージは返されません)。