<div dir="ltr">I created a separate thread to continue this discussion: "grouping / dict of lists"<div><div><br></div><div><span style="color:rgb(51,103,214);text-decoration-line:underline"><a href="https://github.com/selik/peps/blob/master/pep-9999.rst">https://github.com/selik/peps/blob/master/pep-9999.rst</a></span><br></div><div><br></div><div>In my proposal, the update offers a key-function in case the new elements don't follow the same pattern as the existing ones. I can understand the view that the class should retain the key-function from initialization.</div><div><br></div><div>The issue of the group type -- list, set, Counter, etc. -- is handled by offering a Grouping.aggregate method. The Grouping class creates lists, which are passed to the aggregate function. I included examples of constructing sets and Counters.</div><div><br></div><div><div><br><div class="gmail_quote"><div dir="ltr">On Fri, Jun 29, 2018 at 10:04 AM MRAB <<a href="mailto:python@mrabarnett.plus.com">python@mrabarnett.plus.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 2018-06-29 05:14, David Mertz wrote:<br>
> <br>
> Mike Selik asked for my opinion on a draft PEP along these lines. I <br>
> proposed a slight modification to his idea that is now reflected in his <br>
> latest edits. With some details fleshed out, I think this is a promising <br>
> idea. I like the a collections class better, of course, but a dict <br>
> classmethod is still a lot smaller change than new syntax change in <br>
> comprehension.<br>
> <br>
> On Thu, Jun 28, 2018, 8:15 PM David Mertz <<a href="mailto:mertz@gnosis.cx" target="_blank">mertz@gnosis.cx</a> <br>
> <mailto:<a href="mailto:mertz@gnosis.cx" target="_blank">mertz@gnosis.cx</a>>> wrote:<br>
> <br>
[snip]<br>
>     For example (this typed without testing, forgive any typos or thinkos):<br>
> <br>
>     >>> from collections import Grouper # i.e. in Python 3.8+<br>
>     >>> grouped = Grouper(range(7), key=mod_2)<br>
>     >>> grouped<br>
>     Grouper({0: [0, 2, 4, 6], 1: [1, 3, 5]})<br>
>     >>> grouped.update([2, 10, 12, 13], key=mod_2)<br>
 >     >>> grouped<br>
 >     Grouper({0: [0, 2, 4, 6, 2, 10, 12], 1: [1, 3, 5, 13]})<br>
 >     >>> # Updating with no key function groups by identity<br>
 >     >>> # ... is there a better idea for the default key function?<br>
 >     >>> grouped.update([0, 1, 2])<br>
 >     >>> grouped<br>
 >     Grouper({0: [0, 2, 4, 6, 2, 10, 12, 0], 1: [1, 3, 5, 13, 1], 2: [2]})<br>
<br>
I think that if a Grouper instance is created with a key function, then <br>
that key function should be used by the .update method.<br>
<br>
You _could_ possibly override that key function by providing a new one <br>
when updating, but, OTOH, why would you want to? You'd be mixing <br>
different kinds of groupings! So -1 on that.<br>
<br>
>     >>> # Maybe do a different style of update if passed a dict subclass<br>
>     >>> #  - Does a key function make sense here?<br>
>     >>> grouped.update({0: 88, 1: 77})<br>
>     >>> grouped<br>
>     Grouper({0: [0, 2, 4, 6, 2, 10, 12, 0, 88],<br>
>     1: [1, 3, 5, 13, 1, 77],<br>
>     2: [2]})<br>
>     >>> # Avoiding duplicates might sometimes be useful<br>
>     >>> grouped.make_unique()   # better name?  .no_dup()?<br>
>     >>> grouped<br>
>     Grouper({0: [0, 2, 4, 6, 10, 12, 88],<br>
>     1: [1, 3, 5, 13, 77],<br>
>     2: [2]})<br>
> <br>
If you want to avoid duplicates, maybe the grouper should be created <br>
with 'set' as the default factory (see 'defaultdict'). However, there's <br>
the problem that 'list' has .append but 'set' has .add...<br>
>     I think that most of the methods of Counter make sense to include<br>
>     here in appropriately adjusted versions. Converting to a plain<br>
>     dictionary should probably just be `dict(grouped)`, but it's<br>
>     possible we'd want `grouped.as_dict()` or something.<br>
> <br>
>     One thing that *might* be useful is a way to keep using the same key<br>
>     function across updates. Even with no explicit provision, we *could*<br>
>     spell it like this:<br>
> <br>
>     >>> grouped.key_func = mod_2<br>
>     >>> grouped.update([55, 44, 22, 111], key=grouped.key_func)<br>
> <br>
>     Perhaps some more official API for doing that would be useful though.<br>
> <br>
[snip]<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div></div></div></div></div>