Very basic Python loop

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

Very basic Python loop

Ruben Geert van den Berg
Dear all,
 
I'm trying to write a very basic python loop but it doesn't work and I don't understand why. Could anyone please shed any light on this? It should produce a crosstab between the third through the sixth variable in the dataset with gender (yes, I know crosstabs takes variable lists, this is just an attempt to understand loops in Python). Since the spss.Submit is in the loop, I'd expect this to generate 4 separate "crosstabs" commands but I clearly don't get the Python logic yet.
 
begin program.
import spss
for i in range (3,7):
 spss.Submit("crosstabs ",spss.GetVariableName(i)," by gender/cells=column.")
end program.

TIA,

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com





New Windows 7: Simplify what you do everyday. Find the right PC for you.
Reply | Threaded
Open this post in threaded view
|

Re: Very basic Python loop

Wilhelm Landerholm | Queue
Hi!

Try to Change:
    spss.Submit("crosstabs ",spss.GetVariableName(i)," by gender/cells=column.")
To:
    spss.Submit("crosstabs " + spss.GetVariableName(i) + " by gender/cells=column.")

All the best

Wilhelm (Wille) Landerholm
+46-735-460000

Queue/STATB
BOX 92
162 12 Vallingby
Sweden

http://www.qsweden.com
http://www.statb.com

QUEUE/STATB - your partner in data analysis, data modeling and data mining.

Ruben van den Berg skrev 2010-06-11 14:53:
Dear all,
 
I'm trying to write a very basic python loop but it doesn't work and I don't understand why. Could anyone please shed any light on this? It should produce a crosstab between the third through the sixth variable in the dataset with gender (yes, I know crosstabs takes variable lists, this is just an attempt to understand loops in Python). Since the spss.Submit is in the loop, I'd expect this to generate 4 separate "crosstabs" commands but I clearly don't get the Python logic yet.
 
begin program.
import spss
for i in range (3,7):
 spss.Submit("crosstabs ",spss.GetVariableName(i)," by gender/cells=column.")
end program.

TIA,

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com





New Windows 7: Simplify what you do everyday. Find the right PC for you.
Reply | Threaded
Open this post in threaded view
|

Re: Very basic Python loop

Ruben Geert van den Berg
Oh, how stupid of me! Time for weekend I guess...
 
Thanks!

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com



 

Date: Fri, 11 Jun 2010 15:25:24 +0200
From: [hidden email]
To: [hidden email]
CC: [hidden email]
Subject: Re: Very basic Python loop

Hi!

Try to Change:
    spss.Submit("crosstabs ",spss.GetVariableName(i)," by gender/cells=column.")
To:
    spss.Submit("crosstabs " + spss.GetVariableName(i) + " by gender/cells=column.")

All the best

Wilhelm (Wille) Landerholm
+46-735-460000

Queue/STATB
BOX 92
162 12 Vallingby
Sweden

http://www.qsweden.com
http://www.statb.com

QUEUE/STATB - your partner in data analysis, data modeling and data mining.

Ruben van den Berg skrev 2010-06-11 14:53:
Dear all,
 
I'm trying to write a very basic python loop but it doesn't work and I don't understand why. Could anyone please shed any light on this? It should produce a crosstab between the third through the sixth variable in the dataset with gender (yes, I know crosstabs takes variable lists, this is just an attempt to understand loops in Python). Since the spss.Submit is in the loop, I'd expect this to generate 4 separate "crosstabs" commands but I clearly don't get the Python logic yet.
 
begin program.
import spss
for i in range (3,7):
 spss.Submit("crosstabs ",spss.GetVariableName(i)," by gender/cells=column.")
end program.

TIA,

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com





New Windows 7: Simplify what you do everyday. Find the right PC for you.


Express yourself instantly with MSN Messenger! MSN Messenger
Reply | Threaded
Open this post in threaded view
|

Re: Very basic Python loop

Rick Oliver-3
In reply to this post by Ruben Geert van den Berg

Try this:

begin program.
import spss
varlist=[]
varcount=spss.GetVariableCount()
for i in xrange(varcount):
  varlist.append(spss.GetVariableName(i))
for i in range(3,7):
  cmd="crosstabs tables=" + varlist[i] + " by gender /cells=column."
  spss.Submit(cmd)
end program.

But remember that python indexing starts at 0, so the first variable in this range is, in fact, the fourth variable in the file.


From: Ruben van den Berg <[hidden email]>
To: [hidden email]
Date: 06/11/2010 07:55 AM
Subject: Very basic Python loop
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Dear all,

I'm trying to write a very basic python loop but it doesn't work and I don't understand why. Could anyone please shed any light on this? It should produce a crosstab between the third through the sixth variable in the dataset with gender (yes, I know crosstabs takes variable lists, this is just an attempt to understand loops in Python). Since the spss.Submit is in the loop, I'd expect this to generate 4 separate "crosstabs" commands but I clearly don't get the Python logic yet.

begin program.
import spss
for i in range (3,7):
spss.Submit("crosstabs ",spss.GetVariableName(i)," by gender/cells=column.")
end program.

TIA,

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com





New Windows 7: Simplify what you do everyday. Find the right PC for you.

Reply | Threaded
Open this post in threaded view
|

Re: Very basic Python loop

Jon K Peck
In reply to this post by Ruben Geert van den Berg

You are close.  See below.
Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Ruben van den Berg <[hidden email]>
To: [hidden email]
Date: 06/11/2010 06:56 AM
Subject: [SPSSX-L] Very basic Python loop
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Dear all,

I'm trying to write a very basic python loop but it doesn't work and I don't understand why. Could anyone please shed any light on this? It should produce a crosstab between the third through the sixth variable in the dataset with gender (yes, I know crosstabs takes variable lists, this is just an attempt to understand loops in Python). Since the spss.Submit is in the loop, I'd expect this to generate 4 separate "crosstabs" commands but I clearly don't get the Python logic yet.

begin program.
import spss
for i in range (3,7):
spss.Submit("crosstabs ",spss.GetVariableName(i)," by gender/cells=column.")
end program.


Submit needs to build a single command string.  You are passing the parts as three arguments.  Instead, do this
 spss.Submit("crosstabs " + spss.GetVariableName(i) + " by gender/cells=column.")
That will concatenate the parts into a single string and submit the command to Statistics.

HTH,
Jon Peck

TIA,

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com





New Windows 7: Simplify what you do everyday. Find the right PC for you.

Reply | Threaded
Open this post in threaded view
|

Re: Very basic Python loop

Albert-Jan Roskam
Hi Ruben,

That's called operator overloading and I think Python is very strong in that (I really miss it when using R!). Objects of class 'int' have a special method __add__ (which also corresponds to the + sign). But the developers of Python also implemented an __add__ method for objects of class str. Very handy, although it can cause mistakes that do not make your program crash:
>>> 1 + 1
2
>>> "1" + "1"
'11'

Here's another example of overloading:
>>> set(range(1, 10))-set(range(5, 15))
set([1, 2, 3, 4])

Isn't it great? ;-)

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Fri, 6/11/10, Jon K Peck <[hidden email]> wrote:

From: Jon K Peck <[hidden email]>
Subject: Re: [SPSSX-L] Very basic Python loop
To: [hidden email]
Date: Friday, June 11, 2010, 3:41 PM


You are close.  See below.
Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Ruben van den Berg <[hidden email]>
To: [hidden email]
Date: 06/11/2010 06:56 AM
Subject: [SPSSX-L] Very basic Python loop
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Dear all,

I'm trying to write a very basic python loop but it doesn't work and I don't understand why. Could anyone please shed any light on this? It should produce a crosstab between the third through the sixth variable in the dataset with gender (yes, I know crosstabs takes variable lists, this is just an attempt to understand loops in Python). Since the spss.Submit is in the loop, I'd expect this to generate 4 separate "crosstabs" commands but I clearly don't get the Python logic yet.

begin program.
import spss
for i in range (3,7):
spss.Submit("crosstabs ",spss.GetVariableName(i)," by gender/cells=column.")
end program.


Submit needs to build a single command string.  You are passing the parts as three arguments.  Instead, do this
 spss.Submit("crosstabs " + spss.GetVariableName(i) + " by gender/cells=column.")
That will concatenate the parts into a single string and submit the command to Statistics.

HTH,
Jon Peck

TIA,

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com





New Windows 7: Simplify what you do everyday. Find the right PC for you.