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

David Mertz mertz at gnosis.cx
Mon Jul 9 16:38:23 EDT 2018


In my mind, I *rarely* (which is more than never) have my data in the form
of a sequence of key/value pairs.  The version of the API that assumes data
starts that way feels like either a niche case, or demands preprocessing
before it's ready to pass to grouping() or collections.Grouping().

That said, an identity key is rarely interesting either. So I think have
key=None mean "assume we get key/val pairs is harmless to the more common
case where we give an explicit key function.

The uncommon need for grouping on equality can be handled with 'key=lambda
x: x'.

On Mon, Jul 9, 2018, 12:22 PM Chris Barker <chris.barker at noaa.gov> wrote:

> On Fri, Jul 6, 2018 at 12:26 PM, Franklin? Lee I use this kind of function
> in several different projects over the
>>
>> years, and I rewrote it many times as needed.
>>
>
>
>> I added several options, such as:
>> - key function
>> - value function
>> - "ignore": Skip values with these keys.
>> - "postprocess": Apply a function to each group after completion.
>> - Pass in the container to store in. For example, create an
>> OrderedDict and pass it in. It may already hold items.
>> - Specify the container for each group.
>> - Specify how to add to the container for each group.
>>
>
> interesting...
>
>
>> Then I cut it down to two optional parameters:
>> - key function. If not provided, the iterable is considered to have
>> key-value pairs.
>>
>
> OK -- seems we're all converging on that :-)
>
>
>> - The storage container.
>>
>
> so this means you'r passing in a full set of storage containers? I'm a vit
> confused by that -- if they might be pre-populated, then they would need to
> be instance,s an you'd need to have one for every key -- how would you know
> in advance aht you needed???
>
> I played around with passing in a optional storage object:
>
>
> https://github.com/PythonCHB/grouper/commit/d986816905406ec402724beaed2b88c96df64469
>
> but as we might want a list or a set, or a Counter, or ??? it got pretty
> ugly, as lists and sets and Counters all have different APIs for adding
> stuff. So I gave up and figured just saying "it's always a list) made the
> most sense.
>
>
>> Finally, I removed the key function, and only took pairs and an
>> optional container. However, I don't remember why I removed the key
>> function. It may be that I was writing throwaway lambdas, and I
>> decided I might as well just write the transformation into the
>> comprehension.
>
>
> exactly -- but I suspect hat may be because you where writing a
> comprehension anyway, as you needed to manipulate the values, also -- so if
> there were a value function, you could use either API.
>
>
>> I think a key function is worth having.
>>
>
> I think there's more or less consensus on that too.
>
> -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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180709/89009eea/attachment-0001.html>


More information about the Python-ideas mailing list