Python index problem How to subscript into dictionary?

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

Python index problem How to subscript into dictionary?

Art Kendall

Ultimately, I am attempting to plug a variable label into GGRAPH. I.e.,  how to plug a variable label into a command like.
GUIDE: axis(dim(2), label("        "), delta(1), start(-3))
                                            ^^^^
to approach this I am now doing a minimal Python program that just prints the variable name and label.
I found varIndex in the manual but that did not work. 
Traceback (most recent call last):
  File "<string>", line 7, in <module>
  File "C:\PROGRA~1\IBM\SPSS\STATIS~1\22\Python\Lib\site-packages\spssaux\spssaux.py", line 1094, in __getitem__
    raise ValueError, _msg10
ValueError: Variable not in this dictionary
What should I put in the line in red below?

data list list/ year(f4) zscore1 to zscore4 (4f6).
begin data
1993 -.25    -2.08    1.00    -1.33
end data.
variable labels
Zscore1 'some set of words'
Zscore2 'A second set of words'
Zscore3 'third set of words'
Zscore4 'words and words'.

*this works.
begin program.
variables='Zscore1 to Zscore4'
import spss,spssaux
vardict = spssaux.VariableDict()
vList=spssaux.VariableDict().expand(variables)
for vnam in vList:
 print vnam
end program.

*this does not.
begin program.
variables='Zscore1 to Zscore4'
import spss,spssaux
vardict = spssaux.VariableDict()
vList=spssaux.VariableDict().expand(variables)
for vnam in vList:
 vlab = vardict[vnam].VariableLabel
 print vnam, vlab
end program.


-- 
Art Kendall
Social Research Consultants
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: Python index problem How to subscript into dictionary?

Jon K Peck
This small tweak works.
begin program.
variables='zscore1 to zscore4'
import spss,spssaux
vardict = spssaux.VariableDict()
vList=spssaux.VariableDict().expand(variables)
for vnam in vList:
 print vnam
 vlab = vardict[vnam].VariableLabel
 print vnam, vlab
end program.

Remember that the VariableDict object is case sensitive, although the docstring for expand says (correctly)
        case is IGNORED here, unlike elsewhere in this class.

Alternatively you can use a caseless dictionary, which has a little more overhead
vardict = spssaux.VariableDict(caseless=True)
The caseless option was added in 2009.  With an old version of Statistics you might need to download spssaux.py from the SPSS Community site.

Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        Art Kendall <[hidden email]>
To:        [hidden email],
Date:        06/11/2013 03:55 PM
Subject:        [SPSSX-L] Python index  problem  How to subscript into dictionary?
Sent by:        "SPSSX(r) Discussion" <[hidden email]>





Ultimately, I am attempting to plug a variable label into GGRAPH. I.e.,  how to plug a variable label into a command like.
GUIDE: axis(dim(2), label("
       "), delta(1), start(-3))
                                           ^^^^
to approach this I am now doing a minimal Python program that just prints the variable name and label.
I found varIndex in the manual but that did not work.  

Traceback (most recent call last):
 File "<string>", line 7, in <module>
 File "C:\PROGRA~1\IBM\SPSS\STATIS~1\22\Python\Lib\site-packages\spssaux\spssaux.py", line 1094, in __getitem__
   raise ValueError, _msg10
ValueError: Variable not in this dictionary

What should I put in the line in red below?

data list list/ year(f4) zscore1 to zscore4 (4f6).
begin data
1993 -.25    -2.08    1.00    -1.33
end data.
variable labels
Zscore1 'some set of words'
Zscore2 'A second set of words'
Zscore3 'third set of words'
Zscore4 'words and words'.

*this works.
begin program.
variables='Zscore1 to Zscore4'
import spss,spssaux
vardict = spssaux.VariableDict()
vList=spssaux.VariableDict().expand(variables)
for vnam in vList:
print vnam
end program.

*this does not.
begin program.
variables='Zscore1 to Zscore4'
import spss,spssaux
vardict = spssaux.VariableDict()
vList=spssaux.VariableDict().expand(variables)
for vnam in vList:
vlab = vardict[vnam].VariableLabel
print vnam, vlab
end program.


--
Art Kendall
Social Research Consultants

Art Kendall
Social Research Consultants



View this message in context: Python index problem How to subscript into dictionary?
Sent from the
SPSSX Discussion mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

Re: Python index problem How to subscript into dictionary?

Art Kendall
I have looked at this a dozen times.
The only tweak I can spot is the line "print vnam" has been added.
What am I missing?

I am using v22.


begin program.
variables='zscore1 to zscore4'
import spss,spssaux
vardict = spssaux.VariableDict()
vList=spssaux.VariableDict().expand(variables)
for vnam in vList:
 print vnam
 vlab = vardict[vnam].VariableLabel
 print vnam, vlab
end program.


Art Kendall
Social Research Consultants
On 6/11/2013 10:27 PM, Jon K Peck [via SPSSX Discussion] wrote:
This small tweak works.
begin program.
variables='zscore1 to zscore4'
import spss,spssaux
vardict = spssaux.VariableDict()
vList=spssaux.VariableDict().expand(variables)
for vnam in vList:
 print vnam
 vlab = vardict[vnam].VariableLabel
 print vnam, vlab
end program.

Remember that the VariableDict object is case sensitive, although the docstring for expand says (correctly)
        case is IGNORED here, unlike elsewhere in this class.

Alternatively you can use a caseless dictionary, which has a little more overhead
vardict = spssaux.VariableDict(caseless=True)
The caseless option was added in 2009.  With an old version of Statistics you might need to download spssaux.py from the SPSS Community site.

Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        Art Kendall <[hidden email]>
To:        [hidden email],
Date:        06/11/2013 03:55 PM
Subject:        [SPSSX-L] Python index  problem  How to subscript into dictionary?
Sent by:        "SPSSX(r) Discussion" <[hidden email]>





Ultimately, I am attempting to plug a variable label into GGRAPH. I.e.,  how to plug a variable label into a command like.
GUIDE: axis(dim(2), label("
       "), delta(1), start(-3))
                                           ^^^^
to approach this I am now doing a minimal Python program that just prints the variable name and label.
I found varIndex in the manual but that did not work.  

Traceback (most recent call last):
 File "<string>", line 7, in <module>
 File "C:\PROGRA~1\IBM\SPSS\STATIS~1\22\Python\Lib\site-packages\spssaux\spssaux.py", line 1094, in __getitem__
   raise ValueError, _msg10
ValueError: Variable not in this dictionary

What should I put in the line in red below?

data list list/ year(f4) zscore1 to zscore4 (4f6).
begin data
1993 -.25    -2.08    1.00    -1.33
end data.
variable labels
Zscore1 'some set of words'
Zscore2 'A second set of words'
Zscore3 'third set of words'
Zscore4 'words and words'.

*this works.
begin program.
variables='Zscore1 to Zscore4'
import spss,spssaux
vardict = spssaux.VariableDict()
vList=spssaux.VariableDict().expand(variables)
for vnam in vList:
print vnam
end program.

*this does not.
begin program.
variables='Zscore1 to Zscore4'
import spss,spssaux
vardict = spssaux.VariableDict()
vList=spssaux.VariableDict().expand(variables)
for vnam in vList:
vlab = vardict[vnam].VariableLabel
print vnam, vlab
end program.


--
Art Kendall
Social Research Consultants

Art Kendall
Social Research Consultants



View this message in context: Python index problem How to subscript into dictionary?
Sent from the
SPSSX Discussion mailing list archive at Nabble.com.



If you reply to this email, your message will be added to the discussion below:
http://spssx-discussion.1045642.n5.nabble.com/Python-index-problem-How-to-subscript-into-dictionary-tp5720680p5720687.html
To start a new topic under SPSSX Discussion, email [hidden email]
To unsubscribe from SPSSX Discussion, click here.
NAML

Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Automatic reply: Python index problem How to subscript into dictionary?

John McConnell-2

Hi

 

I'll be out of the office today until Friday (14) PM.

 

I will check mail intermittently and get back to you as soon as i can

 

Thanks

John

 

 

 

 

Reply | Threaded
Open this post in threaded view
|

Re: Python index problem How to subscript into dictionary?

Art Kendall
In reply to this post by Jon K Peck
WOW!

It also works if I replace "print vnam" with "junk= vnam"
!!??!!
Art Kendall
Social Research Consultants
On 6/11/2013 10:27 PM, Jon K Peck [via SPSSX Discussion] wrote:
begin program.
variables='zscore1 to zscore4'
import spss,spssaux
vardict = spssaux.VariableDict()
vList=spssaux.VariableDict().expand(variables)
for vnam in vList:
 print vnam
 vlab = vardict[vnam].VariableLabel
 print vnam, vlab
end program.

Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: Python index problem How to subscript into dictionary?

PRogman
Being a Newbie with Python I find it unpredictable to find the variable spelling with initial capital in the 'variables= ...' declaration being forwarded to the output instead of returning the actual spelling in the dictionary.
(Added 'caseless=True' to test different spelling)

/PR

* data list list/ year(f4) zscore1 to zscore4 (4f6).
* begin data...end data.

begin program.
variables='Zscore1 to Zscore4'
import spss,spssaux
vardict = spssaux.VariableDict(caseless=True)
vList=spssaux.VariableDict().expand(variables)
print 'vList =', vList
for vnam in vList:
   vlab = vardict[vnam].VariableLabel
   print vnam, vlab
end program.

Output (note variable spelling):
vList = ['Zscore1', u'zscore2', u'zscore3', 'Zscore4']
Zscore1 some set of words
zscore2 a second set of words
zscore3 third set of words
Zscore4 words and words
Reply | Threaded
Open this post in threaded view
|

Automatic reply: Python index problem How to subscript into dictionary?

Hart, Kimberly (hartkb)

I will be out of office on Thursday, June 13th, with limited access to email. I will respond to your email when I return.

Best, Kim

 

Reply | Threaded
Open this post in threaded view
|

Automatic reply: Python index problem How to subscript into dictionary?

Yifan Lu-2
In reply to this post by PRogman

Thanks for your email, I am out of office on June 13 and will be back on June 14. I will respond to your email as soon as I get a chance. For urgent matters, please contact Kelly Freitas at 916-858-5381.

MailGate made the following annotations
---------------------------------------------------------------------
NOTICE: This message is intended only for the individual to whom it is addressed and may contain information that is confidential or privileged. If you are not the intended recipient, or the employee or person responsible for delivering it to the intended recipient, you are hereby notified that any dissemination, distribution, copying or use is strictly prohibited. If you have received this communication in error, please notify the sender and destroy or delete this communication immediately.
---------------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: Python index problem How to subscript into dictionary?

Jon K Peck
In reply to this post by PRogman
The expand method is the only VariableDict method that ignores case unless you are using a caseless dictionary.  If you are using expand in the context of generating syntax for a Submit call, case won't matter, but for VariableDict purposes it does.  Since Python itself is case sensitive in everything and Statistics is not, there's a bit of a culture clash here as well as some extra overhead for caseless dictionaries.  That overhead won't be detectable unless the dictionary is very large.  

Perhaps I should have implemented expand as case sensitive, but at the time that seemed to make things less helpful.  I suppose an api that returned the correctly cased name for an input argument would be useful, but if this matters, you might as well use a caseless dictionary.

Regards,

Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        PRogman <[hidden email]>
To:        [hidden email],
Date:        06/13/2013 07:47 AM
Subject:        Re: [SPSSX-L] Python index  problem  How to subscript into              dictionary?
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Being a Newbie with Python I find it unpredictable to find the variable
spelling with initial capital in the 'variables= ...' declaration being
forwarded to the output instead of returning the actual spelling in the
dictionary.
(Added 'caseless=True' to test different spelling)

/PR

* data list list/ year(f4) *z*score1 to *z*score4 (4f6).
* begin data...end data.

begin program.
variables='*Z*score1 to *Z*score4'
import spss,spssaux
vardict = spssaux.VariableDict(*caseless=True*)
vList=spssaux.VariableDict().expand(variables)
print 'vList =', vList
for vnam in vList:
  vlab = vardict[vnam].VariableLabel
  print vnam, vlab
end program.

/Output (note variable spelling):/
vList = ['Zscore1', u'zscore2', u'zscore3', 'Zscore4']
*Z*score1 some set of words
*z*score2 a second set of words
*z*score3 third set of words
*Z*score4 words and words



--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Python-index-problem-How-to-subscript-into-dictionary-tp5720680p5720696.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
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