[Tutor] Handling Generator exceptions in Python 2.5

2009-06-19 Thread Joe Python
I have a generator as follows to do list calculations.

*result = [(ListA[i] - ListB[i-1])/ListA[i] for i in range(len(ListA))]*

The above generator, throws  '*ZeroDivisionError*' exception if ListA[i] =
0.
Is there a way to say 'Don't divide by ListA[i] if its equal to 0 (within
that statement)'.

Sorry if this question sounds too stupid.

TIA
Joe
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Handling Generator exceptions in Python 2.5

2009-06-19 Thread Joe Python
Thanks everyone for the responses.

- Joe

On Fri, Jun 19, 2009 at 11:09 AM, vince spicer  wrote:

> Well*
>
> *result = [(ListA[i] - ListB[i-1])/ListA[i] for i in range(len(ListA))*if
> not ListA[i] == 0*]
>
> will exclude any results where listA[i] is 0, if you still want these in
> the result
> you may want to use good'ol for statement instead of list comprehension
>
>
> results = []
> for x in range(len(listA)):
> y = ListA[i] - ListB[i-1]
> if not ListA[i] == 0:
> y = y / ListA[i]
> results.append(y)
>
> print results
>
> Vince
>
>
> On Fri, Jun 19, 2009 at 8:55 AM, Joe Python  wrote:
>
>> I have a generator as follows to do list calculations.
>>
>> *result = [(ListA[i] - ListB[i-1])/ListA[i] for i in range(len(ListA))]*
>>
>> The above generator, throws  '*ZeroDivisionError*' exception if ListA[i]
>> = 0.
>> Is there a way to say 'Don't divide by ListA[i] if its equal to 0 (within
>> that statement)'.
>>
>> Sorry if this question sounds too stupid.
>>
>> TIA
>> Joe
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Sorting Dictionary of Dictionary by certain Value

2008-09-23 Thread Joe Python
Hi Pythonistas,

I have a large dictionary of dictionary (50,000+ keys) which has a structure
as follows:
DoD = {
'flintstones' : {
'husband'   : "fred",
'pal'   : "barney",
'income':  500,
},
'jetsons' : {
'husband'   : "george",
'wife'  : "jane",
'his boy' : "elroy",
'income':  700,
},
'simpsons' : {
'husband'   : "homer",
'wife'  : "marge",
'kid'   : "bart",
'income':  600,
},
};

I want to sort the dictionary by 'income'
Is there an efficient way to do the same.
Thanks in advance.

- Jo
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor