invert dictionary with list &c
Des Small
des.small at bristol.ac.uk
Fri Nov 28 06:51:54 EST 2003
anton muhin <antonmuhin.REMOVE.ME.FOR.REAL.MAIL at rambler.ru> writes:
> Des Small wrote:
> > Des Small <des.small at bristol.ac.uk> writes:
> >
> >>anton muhin <antonmuhin.REMOVE.ME.FOR.REAL.MAIL at rambler.ru> writes:
> > [...]
>
> Or like this:
>
> def dict_update(iter, func, default, d):
> def process_element(d, e):
> d[e[0]] = func(d.get(e[0], default), *e[1:])
> return d
>
> return reduce(process_element, iter, d)
>
> def count(l):
> return dict_update(l, lambda x: x + 1, 0, {})
With these I get:
>>> count(["yes", "yes", "no"])
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in count
File "<stdin>", line 5, in dict_update
File "<stdin>", line 3, in process_element
TypeError: <lambda>() takes exactly 1 argument (3 given)
I tried to write something that would work on more generic arguments,
but I couldn't do it without explicit type checking, so I gave up.
[...]
Des
has now embalmed versions in his utils collection.
--
"[T]he structural trend in linguistics which took root with the
International Congresses of the twenties and early thirties [...] had
close and effective connections with phenomenology in its Husserlian
and Hegelian versions." -- Roman Jakobson
More information about the Python-list
mailing list