|
Dear all,
I've a data file with groups of variables. They look like n1_1 n1_2 n1_3 n2_1 n2_2 n3_1 n3_2 n3_3 n3_4 and so on. I want to build syntax based on the first subscript of n. The range of the second subscript differs between groups (so n1 may run up to n1_5 while n2 may run up to n2_7). So I try to make variable lists like n1_1 to n1_i n2_1 to n2_j and so on. I've 7 groups and I tried to loop (1 through 7) and put the loop variable into a pattern statement but I don't master enough Python to get it done. My attempt, so far, is begin program. import spss,spssaux for i in range(7): for j in range(spss.GetVariableCount()): varlist=spssaux.VariableDict(pattern = "n"+str[i+1]+"_\d+") print varlist end program. And the result is Traceback (most recent call last): File "<string>", line 5, in <module> TypeError: 'type' object is unsubscriptable Could anyone please propose a Python statement that will do the job? 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 Express yourself instantly with MSN Messenger! MSN Messenger |
|
Jon Peck SPSS, an IBM Company [hidden email] 312-651-3435
Dear all, I've a data file with groups of variables. They look like n1_1 n1_2 n1_3 n2_1 n2_2 n3_1 n3_2 n3_3 n3_4 and so on. I want to build syntax based on the first subscript of n. The range of the second subscript differs between groups (so n1 may run up to n1_5 while n2 may run up to n2_7). So I try to make variable lists like n1_1 to n1_i n2_1 to n2_j and so on. I've 7 groups and I tried to loop (1 through 7) and put the loop variable into a pattern statement but I don't master enough Python to get it done. My attempt, so far, is begin program. import spss,spssaux for i in range(7): for j in range(spss.GetVariableCount()): varlist=spssaux.VariableDict(pattern = "n"+str[i+1]+"_\d+") print varlist end program. And the result is Traceback (most recent call last): File "<string>", line 5, in <module> TypeError: 'type' object is unsubscriptable >>>The immediate problem is that str is a function, but this code is subscripting it - square brackets vs parentheses. But the larger problem is that you are creating a variable dictionary for each input variable and each pattern. BTW, when you print it, you will just get a variable dictionary object printed. To see what variables you are creating, change the print statement to print varlist.variables You will see that the code creates each variable list seven times. Try something like this, begin program. import spss,spssaux vardict = spssaux.VariableDict() for i in range(3): #number of groups varlist = vardict.range(pattern="n" + str(i+1) + r"_\d+") print varlist end program. First a variable dictionary is created. (You could make it a subset of the variables starting with n by using pattern="n" in the VariableDict call.) Then the range method of the variableDict object is called to select a subset of the variables in the vardict dictionary. Note that I used r in front of the last part of the pattern string. It isn't necessary here, but it prevents Python from treating \x sequences as escape sequences and ensures that \d is left to be interpreted as regular expression notation. HTH, Jon Peck Express yourself instantly with MSN Messenger! MSN Messenger
|
| Free forum by Nabble | Edit this page |
