[issue45338] Add key argument to collections.Counter

2021-10-01 Thread kubat aytekin


New submission from kubat aytekin :

Counter has become the default tool for counting occurences of objects in an 
iterable. However by construction it only works on hashable objects as it is a 
subclass of dict. Would it be possible to implement a "key=" keyword argument 
as in sort etc.? 

Here's a stackoverflow question wherein either map or implementing a hash 
method was proposed:
https://stackoverflow.com/questions/69401713/elegant-way-of-counting-items-in-a-list-by-enum-value-in-python

--
components: Library (Lib)
messages: 403002
nosy: kubataytekin
priority: normal
severity: normal
status: open
title: Add key argument to collections.Counter
type: enhancement
versions: Python 3.11

___
Python tracker 
<https://bugs.python.org/issue45338>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45338] Add key argument to collections.Counter

2021-10-01 Thread kubat aytekin


kubat aytekin  added the comment:

I was thinking about use cases where one might want to preserve the original 
key. I was not however aware of PEP 455 and the reasons for it's rejection. My 
bad.

Yet It is still unclear to me what the best practice would be to reverse lookup 
unhashable objects. Say you have a list of objects from which you need the most 
common. If there is no good way of reversing the transform, your only option is 
iterating trough your list? Which defeats the purpose of using a Counter and 
you'd be better off counting explicitly. Maybe I'm imagining 
unrealistic/far-fetched use cases.

--

___
Python tracker 
<https://bugs.python.org/issue45338>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45338] Add key argument to collections.Counter

2021-10-01 Thread kubat aytekin


kubat aytekin  added the comment:

Or keep the reverse mapping in another dict:

count = Counter()
reverse_mapping = dict()
for x in all_the_items:
count.update(keyfunc(x))
reverse_mapping[keyfunc(x)] = x

Anyways I'm closing it as it is a partial duplicate of key-transforming 
dictionaries.

--
resolution:  -> duplicate
stage:  -> resolved
status: open -> closed

___
Python tracker 
<https://bugs.python.org/issue45338>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com