[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-

[issue45338] Add key argument to collections.Counter

2021-10-01 Thread Dennis Sweeney
Dennis Sweeney added the comment: The values of a Counter are generally integers, not lists. Maybe you want: items_by_keyfunc = defaultdict(list) for x in all_the_items: items_by_keyfunc[keyfunc(x)].append(x) Then items_by_keyfunc[42] is a list of the things with key 42. Alth

[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 object

[issue45338] Add key argument to collections.Counter

2021-10-01 Thread Karthikeyan Singaravelan
Change by Karthikeyan Singaravelan : -- nosy: +rhettinger ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: htt

[issue45338] Add key argument to collections.Counter

2021-10-01 Thread Dennis Sweeney
Dennis Sweeney added the comment: How is Counter(numbers, key=abs) any better than Counter(map(abs, numbers))? It seems to me that "apply a function to each thing" (map) and "count the numbers of each thing" (Counter) are two orthogonal concepts, and there's no reason to entangle them. Their

[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