Help understanding the decisions *behind* python?
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Thu Jul 23 00:27:12 EDT 2009
En Wed, 22 Jul 2009 11:36:51 -0300, Inky 788 <inky788 at gmail.com> escribió:
> On Jul 22, 2:36 am, Hendrik van Rooyen <hend... at microcorp.co.za>
> wrote:
>> The good reason is the immutability, which lets you use
>> a tuple as a dict key.
>
> Thanks for the reply Hendrik (and Steven (other reply)). Perhaps I'm
> just not sophisticated enough, but I've never wanted to use a list/
> tuple as a dict key. This sounds like obscure usage, and a bit
> contrived as a reason for having *both* lists and tuples.
Many people posted useful examples of tuples as dictionary keys in this
thread. Just to add another one (emulate SQL GROUP BY):
ope_by_dept = defaultdict(int)
total_times = defaultdict(float)
for dept_name, ope_name, ope_date, engineer in list_of_operations:
ope_by_dept[dept_name, ope_start.month] += 1
total_times[dept_name, engineer] += ope_end - ope_start
print "Operations per department per month"
for dept_name, month in sorted(ope_by_dept):
print dept_name, month, ope_by_dept[dept_name, month]
--
Gabriel Genellina
More information about the Python-list
mailing list