spss-python probleam: order of variables in VariableDict

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

spss-python probleam: order of variables in VariableDict

Frans Marcelissen-3
Colleages,

I use python on spss 16. I need subsets of my variables and use VariableDict
for this. But for one reason of another the order of the variables is
disturbed when I use variablesf or expand ("ALL"). This is illustrated
below.

-----

data list free /v1 b3 a4 v2.

begin data

1 2 3 4

1 2 3 4

1 2 3 4

end data.

begin program.

import spssaux

v=spssaux.VariableDict()

print v.variablesf()

print v.expand("ALL")

print v.expand(["v1","TO","v2"])

end program.

['v1', 'v2', 'a4', 'b3']

['v1', 'v2', 'a4', 'b3']

['v1', 'b3', 'a4', 'v2']

----

So the correct order of the variables is v1 b3 a4 v2

Variablesf() en expand("ALL") both give the variable in a strange(?) order
(not the original order, nor alphabetic).

expand(["v1","TO","v2"]) gives the correct order.

Could anyone explain this?

Greetings

Frans



-------------------------------------------

Frans Marcelissen

email [hidden email]

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: spss-python probleam: order of variables in VariableDict

Peck, Jon
See below.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Frans Marcelissen
Sent: Sunday, December 14, 2008 6:50 AM
To: [hidden email]
Subject: [SPSSX-L] spss-python probleam: order of variables in VariableDict

Colleages,

I use python on spss 16. I need subsets of my variables and use VariableDict
for this. But for one reason of another the order of the variables is
disturbed when I use variablesf or expand ("ALL"). This is illustrated
below.
[>>>Peck, Jon] Actually, there is no guarantee that the order of the variables in the variables property or the variablesf function is the same as SPSS dictionary order.  TO is correctly expanded using that order, but otherwise the order is random.  This is done for efficiency reasons in randomly accessing variables by name.

You can, though, get a list in dictionary order in one overly long line of code.

Suppose
vardict = spssaux.VariableDict()

The following expression produces the contents in SPSS dictionary order
[vn for (vi, vn) in sorted(zip(vardict.indexes(), vardict.variables))]

This uses the indexes method of the dictionary class to get the SPSS index number, combines that with the list of names, sorts by index, and returns just the name portion as a list.

Each variable has an index property, too, so, for example,
vardict['b3'].index
tells you the location of variable b3 in the SPSS dictionary.

Also, the iterator method of the VariableDict class uses SPSS order:
for v in vardict:
  print v.VariableName, v.index

In this case, the output would be
v1 0
b3 1
a4 2
v2 3

HTH,
Jon Peck

-----

data list free /v1 b3 a4 v2.

begin data

1 2 3 4

1 2 3 4

1 2 3 4

end data.

begin program.

import spssaux

v=spssaux.VariableDict()

print v.variablesf()

print v.expand("ALL")

print v.expand(["v1","TO","v2"])

end program.

['v1', 'v2', 'a4', 'b3']

['v1', 'v2', 'a4', 'b3']

['v1', 'b3', 'a4', 'v2']

----

So the correct order of the variables is v1 b3 a4 v2

Variablesf() en expand("ALL") both give the variable in a strange(?) order
(not the original order, nor alphabetic).

expand(["v1","TO","v2"]) gives the correct order.

Could anyone explain this?

Greetings

Frans



-------------------------------------------

Frans Marcelissen

email [hidden email]

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD