Searching for most pythonic/least stupid way to do something simple

david jensen dmj.ccc at gmail.com
Tue Mar 16 08:57:05 EDT 2010


Hi all,

This may be a complete brainfart, but it's been puzzling me for a day
or two (!).
Sorry for not describing "something" in the subject, but it's hard to
describe succinctly:

I have a short list of non-zero positive integers (say
myList=[2,5,8,3,5]). I need to return five lists of non-negative
numbers, such that for five different "share sizes", myList[0] and
myList[1] will share twice the smaller amount...

def getOutcomes():
    outcomes=[]
    if myList[0]<=myList[1]:
        amountToShare=2*myList[0]
        remainder=myList[1]-myList[0]
        outcome.append((amountToShare, remainder)+myList[2:])   #
shares are (100%, 0)
        outcome.append((amountToShare*0.75, remainder
+amountToShare*0.25)+myList[2:])   #shares are (75%, 25%), and exactly
the same for (50%,50%), (25%, 75%), and (0,100%)
...
...
    return outcomes

i.e. for the above myList, outcomes=[[4,1,8,3,5], [3,2,8,3,5],
[2,5,8,3,5],[1,6,8,3,5],[0,7,8,3,5]]

if myList[0]>myList[1], i want exactly the opposite to happen (i.e.,
just switching what happens to positions 0 and 1)

Obviously, i can just write the code again, in an else, switching
indices 0 and 1. Or, I could just have a test at the beginning, switch
them if they are in the order "big, small", and then switch them again
at the end in a list comprehension. Both ideas seem terribly silly,
and there must be an obvious way to do it that I'm not seeing.

any help?

many thanks

dmj



More information about the Python-list mailing list