|
Dear all,
I've a list (dropvars) consisting of strings. These reside in string variable v1. I want to remove all cases for which v1 takes on a value in dropvars. The syntax I wish to generate is sel if v1 ne "bli" and v1 ne "blo" and v1 ne "bla". (dropvars=['bli','blo','bla']) I tried spss.Submit(['sel if ']+['v1 ne "'+k for k in dropvars[0:len(dropvars)-2]+'" and']+['v1 ne "'+dropvars[len(dropvars)-1]+'".']) to accomplish this (I though I saw Albert-Jan submitting lists to SPSS) but I get the error TypeError: can only concatenate list (not "str") to list AFAIK, I'm just trying to concatenate 3 lists (the first and last consisting of a single string element). Could anybody please help? Alternative solutions are (obviously!) appreciated as well. TIA! Ruben van den Berg Consultant Models & Methods TNS NIPO E [hidden email] P +31 6 24641435 I www.tns-nipo.com |
|
Don't know anything about Python, but how about something
like this?
count dropvars = v1 ('bli' 'bla' 'blo' ) .
temp .
sel if (dropvars gt 0) .
|
|
Hi John!
I figured I can just use for k in dropvars: spss.Submit("sel if v1 ne '"+k+"'.") using a separate SPSS command for every element in dropvars instead of one single SPSS command. However, the original plan was something akin to cmd = ["add files /file = *"] + [" /file='" + f + "'" for k, f in enumerate(savs) if k < 49] + [".", "exe."] spss.Submit(cmd) Best, Ruben van den Berg Consultant Models & Methods TNS NIPO E [hidden email] P +31 6 24641435 I www.tns-nipo.com From: [hidden email] To: [hidden email]; [hidden email] Subject: Re: What's wrong with this Python line? Date: Thu, 26 Aug 2010 10:48:30 +0200 Don't know anything about Python, but how about something like this?
count dropvars = v1 ('bli' 'bla' 'blo' ) .
temp .
sel if (dropvars gt 0) .
|
|
In reply to this post by Ruben Geert van den Berg
Hi Ruben,
You're trying to concatenate different data types, namely list and str (the dot). That won't work. How about this:
dropvars=['bli','blo','bla']
spss.Submit( "select if (" + " and ".join(["v1 ne " + d for d in dropvars]) + ")." )
so
L1 = [1]
L2 = [2]
L3 = L1 + L2
S1 = "1"
S2 = "2"
S3 = S1 + S2
But not: L1 + S1
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? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From: Ruben van den Berg <[hidden email]> To: [hidden email] Sent: Thu, August 26, 2010 10:12:42 AM Subject: [SPSSX-L] What's wrong with this Python line? Dear all, I've a list (dropvars) consisting of strings. These reside in string variable v1. I want to remove all cases for which v1 takes on a value in dropvars. The syntax I wish to generate is sel if v1 ne "bli" and v1 ne "blo" and v1 ne "bla". (dropvars=['bli','blo','bla']) I tried spss.Submit(['sel if ']+['v1 ne "'+k for k in dropvars[0:len(dropvars)-2]+'" and']+['v1 ne "'+dropvars[len(dropvars)-1]+'".']) to accomplish this (I though I saw Albert-Jan submitting lists to SPSS) but I get the error TypeError: can only concatenate list (not "str") to list AFAIK, I'm just trying to concatenate 3 lists (the first and last consisting of a single string element). Could anybody please help? Alternative solutions are (obviously!) appreciated as well. TIA! Ruben van den Berg Consultant Models & Methods TNS NIPO E [hidden email] P +31 6 24641435 I www.tns-nipo.com |
|
Hi Albert-Jan,
Thanks, but if I run begin program. dropvars=['bli','blo','bla'] print ['hello '+f for f in dropvars] end program. then I concatenate a string ('hello ') to a list as well, right? I did notice that this begin program. dropvars=['bli','blo','bla'] print ['hello '+f for f in dropvars+'.'] end program. Renders the usual error. Lovely solution BTW! Thanks a lot! Ruben van den Berg Consultant Models & Methods TNS NIPO E [hidden email] P +31 6 24641435 I www.tns-nipo.com Date: Thu, 26 Aug 2010 02:55:59 -0700 From: [hidden email] Subject: Re: What's wrong with this Python line? To: [hidden email] Hi Ruben,
You're trying to concatenate different data types, namely list and str (the dot). That won't work. How about this:
dropvars=['bli','blo','bla']
spss.Submit( "select if (" + " and ".join(["v1 ne " + d for d in dropvars]) + ")." )
so
L1 = [1]
L2 = [2]
L3 = L1 + L2
S1 = "1"
S2 = "2"
S3 = S1 + S2
But not: L1 + S1
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? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From: Ruben van den Berg <[hidden email]> To: [hidden email] Sent: Thu, August 26, 2010 10:12:42 AM Subject: [SPSSX-L] What's wrong with this Python line? Dear all, I've a list (dropvars) consisting of strings. These reside in string variable v1. I want to remove all cases for which v1 takes on a value in dropvars. The syntax I wish to generate is sel if v1 ne "bli" and v1 ne "blo" and v1 ne "bla". (dropvars=['bli','blo','bla']) I tried spss.Submit(['sel if ']+['v1 ne "'+k for k in dropvars[0:len(dropvars)-2]+'" and']+['v1 ne "'+dropvars[len(dropvars)-1]+'".']) to accomplish this (I though I saw Albert-Jan submitting lists to SPSS) but I get the error TypeError: can only concatenate list (not "str") to list AFAIK, I'm just trying to concatenate 3 lists (the first and last consisting of a single string element). Could anybody please help? Alternative solutions are (obviously!) appreciated as well. TIA! Ruben van den Berg Consultant Models & Methods TNS NIPO E [hidden email] P +31 6 24641435 I www.tns-nipo.com |
|
Hi Ruben,
This adds a period in two places; I'm not sure where you want it to be:
>>> dropvars=['bli','blo','bla'] Cheers!!>>> print ['hello ' + f + '.' for f in dropvars] + ['.'] ['hello bli.', 'hello blo.', 'hello bla.', '.'] >>> 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? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From: Ruben van den Berg <[hidden email]> To: [hidden email] Sent: Thu, August 26, 2010 12:37:35 PM Subject: Re: [SPSSX-L] What's wrong with this Python line? Hi Albert-Jan, Thanks, but if I run begin program. dropvars=['bli','blo','bla'] print ['hello '+f for f in dropvars] end program. then I concatenate a string ('hello ') to a list as well, right? I did notice that this begin program. dropvars=['bli','blo','bla'] print ['hello '+f for f in dropvars+'.'] end program. Renders the usual error. Lovely solution BTW! Thanks a lot! Ruben van den Berg Consultant Models & Methods TNS NIPO E [hidden email] P +31 6 24641435 I www.tns-nipo.com Date: Thu, 26 Aug 2010 02:55:59 -0700 From: [hidden email] Subject: Re: What's wrong with this Python line? To: [hidden email] Hi Ruben,
You're trying to concatenate different data types, namely list and str (the dot). That won't work. How about this:
dropvars=['bli','blo','bla']
spss.Submit( "select if (" + " and ".join(["v1 ne " + d for d in dropvars]) + ")." )
so
L1 = [1]
L2 = [2]
L3 = L1 + L2
S1 = "1"
S2 = "2"
S3 = S1 + S2
But not: L1 + S1
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? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From: Ruben van den Berg <[hidden email]> To: [hidden email] Sent: Thu, August 26, 2010 10:12:42 AM Subject: [SPSSX-L] What's wrong with this Python line? Dear all, I've a list (dropvars) consisting of strings. These reside in string variable v1. I want to remove all cases for which v1 takes on a value in dropvars. The syntax I wish to generate is sel if v1 ne "bli" and v1 ne "blo" and v1 ne "bla". (dropvars=['bli','blo','bla']) I tried spss.Submit(['sel if ']+['v1 ne "'+k for k in dropvars[0:len(dropvars)-2]+'" and']+['v1 ne "'+dropvars[len(dropvars)-1]+'".']) to accomplish this (I though I saw Albert-Jan submitting lists to SPSS) but I get the error TypeError: can only concatenate list (not "str") to list AFAIK, I'm just trying to concatenate 3 lists (the first and last consisting of a single string element). Could anybody please help? Alternative solutions are (obviously!) appreciated as well. TIA! Ruben van den Berg Consultant Models & Methods TNS NIPO E [hidden email] P +31 6 24641435 I www.tns-nipo.com |
|
I see. The thing is that the [for f in...] part should come after all concatenations involving [f].
Well, thanks a lot for another valuable lesson! Ruben van den Berg Consultant Models & Methods TNS NIPO E [hidden email] P +31 6 24641435 I www.tns-nipo.com Date: Thu, 26 Aug 2010 04:05:14 -0700 From: [hidden email] Subject: Re: What's wrong with this Python line? To: [hidden email] Hi Ruben,
This adds a period in two places; I'm not sure where you want it to be:
>>> dropvars=['bli','blo','bla'] Cheers!!>>> print ['hello ' + f + '.' for f in dropvars] + ['.'] ['hello bli.', 'hello blo.', 'hello bla.', '.'] >>> 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? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From: Ruben van den Berg <[hidden email]> To: [hidden email] Sent: Thu, August 26, 2010 12:37:35 PM Subject: Re: [SPSSX-L] What's wrong with this Python line? Hi Albert-Jan, Thanks, but if I run begin program. dropvars=['bli','blo','bla'] print ['hello '+f for f in dropvars] end program. then I concatenate a string ('hello ') to a list as well, right? I did notice that this begin program. dropvars=['bli','blo','bla'] print ['hello '+f for f in dropvars+'.'] end program. Renders the usual error. Lovely solution BTW! Thanks a lot! Ruben van den Berg Consultant Models & Methods TNS NIPO E [hidden email] P +31 6 24641435 I www.tns-nipo.com Date: Thu, 26 Aug 2010 02:55:59 -0700 From: [hidden email] Subject: Re: What's wrong with this Python line? To: [hidden email] Hi Ruben,
You're trying to concatenate different data types, namely list and str (the dot). That won't work. How about this:
dropvars=['bli','blo','bla']
spss.Submit( "select if (" + " and ".join(["v1 ne " + d for d in dropvars]) + ")." )
so
L1 = [1]
L2 = [2]
L3 = L1 + L2
S1 = "1"
S2 = "2"
S3 = S1 + S2
But not: L1 + S1
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? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From: Ruben van den Berg <[hidden email]> To: [hidden email] Sent: Thu, August 26, 2010 10:12:42 AM Subject: [SPSSX-L] What's wrong with this Python line? Dear all, I've a list (dropvars) consisting of strings. These reside in string variable v1. I want to remove all cases for which v1 takes on a value in dropvars. The syntax I wish to generate is sel if v1 ne "bli" and v1 ne "blo" and v1 ne "bla". (dropvars=['bli','blo','bla']) I tried spss.Submit(['sel if ']+['v1 ne "'+k for k in dropvars[0:len(dropvars)-2]+'" and']+['v1 ne "'+dropvars[len(dropvars)-1]+'".']) to accomplish this (I though I saw Albert-Jan submitting lists to SPSS) but I get the error TypeError: can only concatenate list (not "str") to list AFAIK, I'm just trying to concatenate 3 lists (the first and last consisting of a single string element). Could anybody please help? Alternative solutions are (obviously!) appreciated as well. TIA! Ruben van den Berg Consultant Models & Methods TNS NIPO E [hidden email] P +31 6 24641435 I www.tns-nipo.com |
|
In reply to this post by Ruben Geert van den Berg
The problem is actually with the Python syntax, not the api usage. You can concatenate two lists with +: [1,2]+[3,4], but you can't concatenate a list and a string: [1,2]+'and' gives an error. You can do it this way, though: [1,2]+['and'] The Submit api will convert a list passed as the argument into a string, but the error is occurring before Submit is ever called. HTH, Jon Peck SPSS, an IBM Company [hidden email] 312-651-3435
Dear all, I've a list (dropvars) consisting of strings. These reside in string variable v1. I want to remove all cases for which v1 takes on a value in dropvars. The syntax I wish to generate is sel if v1 ne "bli" and v1 ne "blo" and v1 ne "bla". (dropvars=['bli','blo','bla']) I tried spss.Submit(['sel if ']+['v1 ne "'+k for k in dropvars[0:len(dropvars)-2]+'" and']+['v1 ne "'+dropvars[len(dropvars)-1]+'".']) to accomplish this (I though I saw Albert-Jan submitting lists to SPSS) but I get the error TypeError: can only concatenate list (not "str") to list
|
|
In reply to this post by Ruben Geert van den Berg
At 04:12 AM 8/26/2010, Ruben van den Berg wrote:
I've a list (dropvars) consisting of strings. These reside in string variable v1. I want to remove all cases for which v1 takes on a value in dropvars. The syntax I wish to generate is Not about Python, except that if you're generating code programmatically, simpler code is simpler to generate -- the simplest native syntax for what you want is probably SELECT IF NOT ANY(v1,"bli","blo","bla"). I don't know whether Python buys you much in generating this. ===================== 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 |
|
Dear Richard,
You couldn't be more right! Very simply, I could have written begin program. dropvars=['bli','blo','bla'] spss.Submit("select if not(any(bla,"+','.join(['"'+k+'"' for k in dropvars])+""")). execute.""") end program. But after heaps of confusion I came up with begin program. dropvars=['bli','blo','bla'] spss.Submit(' '.join(["select if "]+["bla ne '"+k+"'and " for k in dropvars[0:len(dropvars)-1]]+["bla ne '"+dropvars[len(dropvars)-1]+"'."])) end program. which looks awkward because it seems like the last element of dropvars is included twice, which isn't the case because...well, nevermind, time for a long holiday ... Thanks! Ruben van den Berg Consultant Models & Methods TNS NIPO E [hidden email] P +31 6 24641435 I www.tns-nipo.com Date: Thu, 26 Aug 2010 20:20:58 -0400 To: [hidden email]; [hidden email] From: [hidden email] Subject: Re: What's wrong with this Python line? At 04:12 AM 8/26/2010, Ruben van den Berg wrote: I've a list (dropvars) consisting of strings. These reside in string variable v1. I want to remove all cases for which v1 takes on a value in dropvars. The syntax I wish to generate is Not about Python, except that if you're generating code programmatically, simpler code is simpler to generate -- the simplest native syntax for what you want is probably SELECT IF NOT ANY(v1,"bli","blo","bla"). I don't know whether Python buys you much in generating this. |
|
Hi
Richard,
it is always
a good idea not to develop in SPSS Syntax Editor when doing Python cause
there you do not have any support concerning Python syntax highlighting and
indentation. If you copy your statements to IDLE, PythonWin or WingIDE you will
see also instantly what might be wrong.
Also, it is a
good idea not to build complex statements that you can not understand right
away. You could use statements like this
cmd =
""
cmd +=
"SELECT IF (NOT ANY(bla, " # cmd += "something" is equivalent to cmd = cmd +
"something"
cmd += ",
".join(dropvars)
cmd += ")) .
\n" # \n inserts a line break
cmd +=
"EXECUTE . \n"
spss.Submit(cmd)
This should
you give clear, easy to read, understandable and maintainable
code.
I did not
test the above script, but it should get you started in finding the bug and
improving your script.
HTH
Best
regards
Georg
Von: SPSSX(r) Discussion [mailto:[hidden email]] Im Auftrag von Ruben van den Berg Gesendet: Freitag, 27. August 2010 10:48 An: [hidden email] Betreff: Re: What's wrong with this Python line? You couldn't be more right! Very simply, I could have written begin program. dropvars=['bli','blo','bla'] spss.Submit("select if not(any(bla,"+','.join(['"'+k+'"' for k in dropvars])+""")). execute.""") end program. But after heaps of confusion I came up with begin program. dropvars=['bli','blo','bla'] spss.Submit(' '.join(["select if "]+["bla ne '"+k+"'and " for k in dropvars[0:len(dropvars)-1]]+["bla ne '"+dropvars[len(dropvars)-1]+"'."])) end program. which looks awkward because it seems like the last element of dropvars is included twice, which isn't the case because...well, nevermind, time for a long holiday ... Thanks! Ruben van den Berg Consultant Models & Methods TNS NIPO E [hidden email] P +31 6 24641435 I www.tns-nipo.com Date: Thu, 26 Aug 2010 20:20:58 -0400 To: [hidden email]; [hidden email] From: [hidden email] Subject: Re: What's wrong with this Python line? At 04:12 AM 8/26/2010, Ruben van den Berg wrote: I've a list (dropvars) consisting of strings. These reside in string variable v1. I want to remove all cases for which v1 takes on a value in dropvars. The syntax I wish to generate is Not about Python, except that if you're generating code programmatically, simpler code is simpler to generate -- the simplest native syntax for what you want is probably SELECT IF NOT ANY(v1,"bli","blo","bla"). I don't know whether Python buys you much in generating this. |
| Free forum by Nabble | Edit this page |
