[Python-ideas] Fwd: grouping / dict of lists

David Mertz mertz at gnosis.cx
Sun Jul 1 22:28:51 EDT 2018


Oh, it looks like he has modified the PEP and taken out the examples of
conversion. That's too bad, hopefully they'll be added back.

But it's pretty simple. Whether my idea of collections.Grouping is adapted
or whether a function/classmethod grouping() produces a plain dictionary,
the casting would be the same:

{k:set(v) for k,v in deps.items()}

{k:Counter(v) for k,v in deps.items()}



On Sun, Jul 1, 2018, 10:12 PM David Mertz <mertz at gnosis.cx> wrote:

> Michael changed from set to list at my urging. A list is more general. A
> groupby in Pandas or SQL does not enforce uniqueness, but DOES preserve
> order. I think the PEP is not fully updated, but it's a list everywhere in
> the proposal itself, just not in the "old techniques."
>
> Moreover,  Michael gives example of "casting" the Grouping to a dictionary
> with either sets or Counters as values. Both are useful, and both can be
> derived from list. But you cannot go backwards from either to the list. The
> transformation is simple and obvious, and can be included in eventual
> documentation.
>
> It really is better to construct the collection using lists—in the fully
> general manner—and then only throw away the generality when that
> appropriate.
>
> On Sun, Jul 1, 2018, 8:47 PM Chris Barker via Python-ideas <
> python-ideas at python.org> wrote:
>
>>
>>
>> Ivan,
>>
>> Did you mean this to go to the list? I hope so, as I've cc-d it this time
>> :-)
>>
>> On Sun, Jul 1, 2018 at 1:20 AM, Ivan Levkivskyi <levkivskyi at gmail.com>
>> wrote:
>>
>>> On 1 July 2018 at 06:18, Chris Barker via Python-ideas <
>>> python-ideas at python.org> wrote:
>>>
>>>> I'm really warming to the:
>>>>
>>>> Alternate: collections.Grouping
>>>>
>>>> version -- I really like this as a kind of custom mapping, rather than
>>>> "just a function" (or alternate constructor) --
>>>>
>>>
>> I wanted the group to be represented as a set, not a list. I however
>>> understand that list may be more common. Can we design an API that
>>> would make this configurable? Something like:
>>>
>>>     from collections import Grouping
>>>
>>>     deps = Grouping(set)  # list can be the default
>>>     deps.update(other_deps)  # uses set.update or list.extend for every
>>> key
>>>     deps.add(trigger, target)  # uses set.add or list.append
>>>
>>
>> yeah, I thought about that too -- Michael was using set() in some of his
>> examples.
>>
>> But the question is -- do we have a single switchable version or just too
>> classes?
>>
>>
>>> Probably allowing an arbitrary collection for values is to general/hard.
>>>
>>
>> maybe not -- if we had the criteria that you pass in any collection you
>> wanted, as long as it had either an .append() or .add()method, it would
>> be pretty easy to do with duck typing magic.
>>
>> Sure -- a user could make a mess easily enough by passing in a weird
>> custom class, but so what? Using something other than a set or list would
>> be a "at your own risk" thing anyway.
>>
>>
>>> Maybe we can just add a flag `unique=True` to the constructor, that will
>>> cause using sets instead of lists for groups?
>>>
>>
>> That's another, more robust, but less flexible option.
>>
>> Stay tuned for a prototype, if I can get it done fast....
>>
>> -CHB
>>
>>
>> --
>>
>> Christopher Barker, Ph.D.
>> Oceanographer
>>
>> Emergency Response Division
>> NOAA/NOS/OR&R            (206) 526-6959   voice
>> 7600 Sand Point Way NE   (206) 526-6329   fax
>> Seattle, WA  98115       (206) 526-6317   main reception
>>
>> Chris.Barker at noaa.gov
>>
>>
>>
>> --
>>
>> Christopher Barker, Ph.D.
>> Oceanographer
>>
>> Emergency Response Division
>> NOAA/NOS/OR&R            (206) 526-6959   voice
>> 7600 Sand Point Way NE   (206) 526-6329   fax
>> Seattle, WA  98115       (206) 526-6317   main reception
>>
>> Chris.Barker at noaa.gov
>> _______________________________________________
>> 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/
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180701/a41e86f5/attachment-0001.html>


More information about the Python-ideas mailing list