Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement: dictionary maths #63626

Closed
RichardNeill mannequin opened this issue Oct 28, 2013 · 4 comments
Closed

enhancement: dictionary maths #63626

RichardNeill mannequin opened this issue Oct 28, 2013 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@RichardNeill
Copy link
Mannequin

RichardNeill mannequin commented Oct 28, 2013

BPO 19427
Nosy @birkenfeld, @ericvsmith, @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2013-10-29.06:46:15.227>
created_at = <Date 2013-10-28.23:16:08.836>
labels = ['interpreter-core', 'type-feature']
title = 'enhancement: dictionary maths'
updated_at = <Date 2013-10-29.06:57:41.137>
user = 'https://bugs.python.org/RichardNeill'

bugs.python.org fields:

activity = <Date 2013-10-29.06:57:41.137>
actor = 'georg.brandl'
assignee = 'none'
closed = True
closed_date = <Date 2013-10-29.06:46:15.227>
closer = 'r.david.murray'
components = ['Interpreter Core']
creation = <Date 2013-10-28.23:16:08.836>
creator = 'Richard.Neill'
dependencies = []
files = []
hgrepos = []
issue_num = 19427
keywords = []
message_count = 4.0
messages = ['201587', '201588', '201606', '201607']
nosy_count = 4.0
nosy_names = ['georg.brandl', 'eric.smith', 'r.david.murray', 'Richard.Neill']
pr_nums = []
priority = 'normal'
resolution = 'postponed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue19427'
versions = ['Python 3.5']

@RichardNeill
Copy link
Mannequin Author

RichardNeill mannequin commented Oct 28, 2013

It would be really nice if python supported mathematical operations on dictionaries. This is widely requested (eg lots of stackoverflow queries), but there's currently no simple way to do it.

I propose that this should work in the "obvious" way, i.e. for every key in common between the dicts, apply the operator to the set of values. If the dicts have some keys that are not in common, then create the missing keys, with a value of zero (which does sensible things for +,-,* and will throw an error if missing element is a divisor).

It should also allow a dict to be added/multiplied etc with a scalar.

If the dict contains anything other than key:value pairs (i.e. the values are non-scalar types), then this should throw an error.

For example:

>>> d1 = { 'a':1, 'b':2, 'c':3}
>>> d2 = { 'a':4, 'b':5, 'c':6}
>>> d3 = d1 + d2
>>> d3
{'a': 5, 'b': 7, 'c': 9}

>>> d4 = d1 + 17
>>> d4 
{'a': 18, 'b': 19, 'c': 20}

>>> d5 = { 'a':4, 'b':5, 'x':6}
>>> d6 = d1 + d5
>>> d6
{'a': 5, 'b': 7, 'c': 3, 'x': 6}

Perhaps this might need an "import dictionarymath" to enable the behaviour.

Thanks for your consideration of my idea.

@RichardNeill RichardNeill mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Oct 28, 2013
@ericvsmith
Copy link
Member

I suggest you get consensus on this idea on the python-ideas mailing list first.

@bitdancer
Copy link
Member

In the meantime the issue should be closed. It can be reopened if consensus is reached.

@birkenfeld
Copy link
Member

This specialized use case has no chance of being added to the very general dict object.

But have a look at the collections.Counter class which does exactly what you propose for positive values and the +/- operations.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants