|
Dear Listers,
we have begun to use Python for one of our research projects. We need to create sorted variables lists. The variable have indexes in the variable name at 3 positions: The generic format of the variable is q411_Index1_Index2_Index3_07. Example: q411_101_101_101_07 q411_101_102_101_07 q411_101_103_101_07 q411_101_101_102_07 q411_101_102_102_07 q411_101_103_102_07 q411_101_101_103_07 q411_101_102_103_07 q411_101_103_103_07 q411_102_101_101_07 q411_102_102_101_07 q411_102_103_101_07 q411_102_101_102_07 q411_102_102_102_07 q411_102_103_102_07 q411_102_101_103_07 q411_102_102_103_07 q411_102_103_103_07 and so on .... We need to sort the variables first by index1 second by index3 third by index2 The variables are stored in a list like [q411_101_101_101_07, q411_101_102_101_07, q411_101_103_101_07, q411_101_101_102_07, q411_101_102_102_07, q411_101_103_102_07, .....]. How can we sort that list using Python sorting functionality? Best regards Georg Maubach Research Manager |
|
Dear Listers,
we resend our question to this list because the mail system rejected at the first try saying we have asked this question before. We checked our files and found we did not. Maybe that the mail system had a similar mail in the records. ---------------- Dear Listers, we have begun to use Python for one of our research projects. We need to create sorted variables lists. The variable have indexes in the variable name at 3 positions: The generic format of the variable is q411_Index1_Index2_Index3_07. Example:=20 q411_101_101_101_07 q411_101_102_101_07 q411_101_103_101_07 q411_101_101_102_07 q411_101_102_102_07 q411_101_103_102_07 q411_101_101_103_07 q411_101_102_103_07 q411_101_103_103_07 q411_102_101_101_07 q411_102_102_101_07 q411_102_103_101_07 q411_102_101_102_07 q411_102_102_102_07 q411_102_103_102_07 q411_102_101_103_07 q411_102_102_103_07 q411_102_103_103_07 and so on .... We need to sort the variables first by index1 second by index3 third by index2 The variables are stored in a list like [q411_101_101_101_07, q411_101_102_101_07, q411_101_103_101_07, q411_101_101_102_07, q411_101_102_102_07, q411_101_103_102_07, .....]. How can we sort that list using Python sorting functionality? Best regards Georg Maubach Research Manager |
|
This can be done very compactly in Python by making use of its special support for customizing the sorting of lists. When you use the sort method of a list object, you can specify a key function that prepares the list items for comparison. The following will do what you want.
def keyf(x): xs = x.split("_") return "_".join([xs[1], xs[3], xs[2]]) That function extracts the three indexes, rearranges the order, and then joins them back together with the "_". Sorting that rearranged string in the standard way then gives you the sorted list. To use it, if lis is your list of names, do this. lis.sort(key=keyf) In your example the first part of a name is always q411 and the last part is always 07. If these can vary, just include xs[0] and xs[4] in the join list above. Regards, Jon Peck -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Georg Maubach Sent: Thursday, August 30, 2007 2:45 AM To: [hidden email] Subject: [SPSSX-L] Python Sorting Dear Listers, we resend our question to this list because the mail system rejected at the first try saying we have asked this question before. We checked our files and found we did not. Maybe that the mail system had a similar mail in the records. ---------------- Dear Listers, we have begun to use Python for one of our research projects. We need to create sorted variables lists. The variable have indexes in the variable name at 3 positions: The generic format of the variable is q411_Index1_Index2_Index3_07. Example:=20 q411_101_101_101_07 q411_101_102_101_07 q411_101_103_101_07 q411_101_101_102_07 q411_101_102_102_07 q411_101_103_102_07 q411_101_101_103_07 q411_101_102_103_07 q411_101_103_103_07 q411_102_101_101_07 q411_102_102_101_07 q411_102_103_101_07 q411_102_101_102_07 q411_102_102_102_07 q411_102_103_102_07 q411_102_101_103_07 q411_102_102_103_07 q411_102_103_103_07 and so on .... We need to sort the variables first by index1 second by index3 third by index2 The variables are stored in a list like [q411_101_101_101_07, q411_101_102_101_07, q411_101_103_101_07, q411_101_101_102_07, q411_101_102_102_07, q411_101_103_102_07, .....]. How can we sort that list using Python sorting functionality? Best regards Georg Maubach Research Manager |
| Free forum by Nabble | Edit this page |
