On 07/28/2011 07:05 PM, Prasad, Ramit wrote:
I have 2 questions.
1. Is there a way to do a reverse and a normal sort at the same time?
I have a list of tuples (there are more than 2 elements in the tuples but I
only want to sort by the first two). I want to sort in reverse for the first
element (int) and in order for the second element (string).
Example: [ (1,3) (5, 2), (5, 1), (1, 1) ].
The output should be:[ (5,1), (5,2), (1,1), (1,3) ]
I can create a hack for this by sorting both values in order but it is a hack and not a
"Good" solution (untested):
sorted( lst, key=lambda x: (9999999999-x[0],x[1]) )
First point. Nothing wrong with your solution, but you can leave out
the 999999 part. Just use -x[0]
Another option. Do two sorts, first on the secondary key, then on the
primary (reversed). Since Python's sort is stable,
it'll do the right thing.
2. How would you programmatically get a variable when there is no class
involved? If I was in a class I could do something like
programmatically_determined_variable = getattr( self, var_basename +
str(number) )
How would I do this from a module function or directly in the module? And is the solution
"safe"?
Ramit
globals() is a dictionary of all the global variables, so you can do the
same getaddr() technique.
DaveA
--
DaveA
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor