Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

compute concat permutations python

6 views
Skip to first unread message

Lance Hoffmeyer

unread,
Feb 4, 2010, 1:17:30 PM2/4/10
to
Hay all,

i am computing a new set of variables of all combinations from
four sets of variables.

DEFINE !CreateVar()
VECTOR A = A1 to A3.
VECTOR B = B1 to B2.
VECTOR C = C1 to C2.
VECTOR D = D1 to D2.
!DO !i = 1 !TO 3
!DO !j = 1 !TO 2
!DO !k = 1 !TO 2
!DO !L = 1 !TO 2
COMPUTE !CONCAT("PERM",!i,!j,!k,!L) = sum(A(!i),B(!j),C(!k),D(!L)).
!DOEND
!DOEND
!DOEND
!DOEND
EXE.
!ENDDEFINE.
!CreateVar.


I am trying to better understand the python integration with SPSS.

It would probably take me anywhere from two to four days with lots of
cigarettes and more than a couple of shots of vodka to figure out how
to translate this into python

I bet there are a couple of people out there who can translate this in
a matter of minutes without barely even thinking about it.

Anyone care to take a stab at translating this to to a python script?


Thought about not showering/shaving for a few days and then standing on
a street corner with a sign

***************************************.
Couldn't figure out how to translate
SPSS macro to python.

Will work for script
***************************************.

but figured this was easier!, LOL

Too much coffee this morning.

TIA,

Lance


JKPeck

unread,
Feb 4, 2010, 3:01:48 PM2/4/10
to

Here's the gist of the solution. I think what you really want is the
cartesian product of all the variables.

from itertools import product

lisa=['a1','a2','a3']
lisb = ['b1','b2']
lisc = ['c1', 'c2']
lisd = ['d1','d2']
for item in product(lisa, lisb, lisc, lisd):
print item

On each iteration you get a list of length 4 with one item from each
of the component lists. You can then use those variable names as
desired. For example,
lhs = "".join(item)
rhs = ",".join(item)
spss.Submit("COMPUTE %s = sum(%s)" % (lhs, rhs))

If you needed to exand the TO expressions to get those lists, you
could use the expand method of the
spssaux.VariableDict
class. E.g.,
vardict = spssaux.VariableDict()
lisa = vardict.expand("a1 to a3")

HTH,
Jon Peck

0 new messages