[Python-ideas] Delivery Status Notification (Failure)
Jacco van Dorp
j.van.dorp at deonet.nl
Thu Jun 28 02:48:19 EDT 2018
Have you tried just instantiating a Counter() instead ? All missing
keys are considerd to be 0 in a fresh counter. So for example:
>>> c = Counter()
>>> c["a"] += 1
>>> c
Counter({'a': 1})
>>> c["b"]
0
works exactly this way. Which means there's no difference between what
you're suggesting Counter.fromkeys(key-list-or-set, 0) should do and
what Counter() actually does.
(Resend because somehow gmail tried to reply to the wrong python-ideas)
2018-06-27 22:36 GMT+02:00 Abe Dillon <abedillon at gmail.com>:
> Consider the following update function for conway's game of life:
>
> from collections import Counter
>
> def update(live: Set[Tuple[Integer, Integer]]):
> counts = Counter.fromkeys(live, 0) +
> Counter(itertools.chain(neighbors(*cell) for cell in live))
> flip = {cell for cell, count in counts.items()
> if (cell in live and not 1<count<4)
> or (cell not in live and count==3)}
> live ^= flip
>
> around = frozenset(filter(any, itertools.product(range(-1,2), range(-1,2))))
> def neighbors(r: Integer, c: Integer):
> return (((r+dr)%height, (c+dc)%width) for dr, dc in around)
>
> The problem is, Count.fromkeys isn't implemented. I propose that it work
> exactly as it does for dict, otherwise it's difficult to add items to a
> Counter when you want them to start off at zero or some other count.
>
> The best solution I came up with is to, more confusingly, count live cells
> once extra and adjust the rules accordingly:
>
> def update(live: Set[Tuple[Integer, Integer]]):
> counts = Counter(itertools.chain(live, *(neighbors(*cell) for cell in
> live)))
> flip = {cell for cell, count in counts.items()
> if (cell in live and not 2<count<5)
> or (cell not in live and count==3)}
> live ^= flip
>
> around = frozenset(filter(any, itertools.product(range(-1,2), range(-1,2))))
> def neighbors(r: Integer, c: Integer):
> return (((r+dr)%height, (c+dc)%width) for dr, dc in around)
>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
More information about the Python-ideas
mailing list