[Tutor] Setting thresholds in a compact way
Mats Wichmann
mats at wichmann.us
Wed Jan 1 15:08:06 EST 2020
On 1/1/20 9:20 AM, Robert Alexander wrote:
> Not even quite sure that using floats (albeit single decimal) as dictionary
> keys will be ok.
It's not ideal - it isn't illegal, but comparison of binary floats can
be trickier than you expect (not just in Python!), and because
dictionary lookups are based on hashing the key, you can get interesting
problems.
It's fairly easy to mitigate that problem - round your threshold value,
or use decimal floats, or use the old trick of multiplying by 10 and
calling it an int - that is, for 0.4, use 4, etc.
dictionaries, however, aren't really that natural a fit for this kind of
thing. They're perfect when you're doing an exact match - give me the
value for key "foo". But you want find where you fall in ranges instead.
As others have said, I think the if/elif sequence would be fine for
this, although it will look ugly if you have too many different buckets.
More information about the Tutor
mailing list