(Very Newbie) Problems defining a variable

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Fri Dec 12 20:31:09 CET 2008


Tim Rowe a écrit :
> 2008/12/12 Kirk Strauser <kirk at daycos.com>:
> 
>> def get_rate(balance):
>>    for threshold, rate in ((100000, .0173),
>>                            (50000, .0149),
>>                            (25000, .0124),
>>                            (10000, .0085),
>>                            (0, .006)):
>>        if balance > threshold:
>>            return rate
>>    return .0
> 
> Yes, once it's changed from a dictionary to tuples it becomes easier,
> doesn't it? D'oh!
> 

<comment>
A sequence of pairs and a dict are _almost_ interchangeable (mmm... is 
that the correct word ?) representations of a same data set[1] - the 
main difference being ordering. If ordering matters, choose a sequence 
of pairs as main representation - you can easily build a dict from it 
if/when you need it.

[1]
 >>> d = dict(a=1, b=2, c=3)
 >>> dict(d.items()) == d
True
 >>>
</comment>



More information about the Python-list mailing list