Dictionary Keys treated as Sets
related to: pep-0584, PEP-0218, and ??? The request is that since dictionary keys can not be duplicated / are unique that they may behave the same as sets - and have the set operators actions and return sets. eg.: aSet = set('a','b','c','bar') aDict = {'a':'aaa', 'b':'baa' , 'foo':fou' } bDict = {'a':'ea', 'b':'bee' , 'foo':fuh', 'bar': 'drink' } aSet | aDict | bDict = set('a','b','c','foo','bar') bDict & aDict =set('a','b','foo') bDict - aSet =set('foo') aDict^bDict =set('bar') The dictionary class would support set filtering: New_dict_from_set = { your_key: old_dict[your_key] for your_key in aSET } e.g. a function such as: (taking the intersection) aDict.Subdictionary(aSet) ={'a':'aaa', 'b':'baa'} set(dictionary) would return the Keys (as a mutable). Adding an element to the mutable set would add a None as a value to the dictionary. The "-=", "-", "+", "+=" would behave as specified in pep-0584 if both are dictionaries, but if one of the operands is a set - a set is returned. The behaviour of bDict.Subdictionary(aDict) = {'a':'ea', 'b':'bee' , 'foo':fuh') i.e. treats aDict as a set aDict.Subdictionary(List)== aDict.Subdictionary(Set(List))
-1 strongly. This will hide crucial type errors. One could argue that aDict.keys() could behave as a set though.
On 17 Sep 2019, at 17:03, Rupert Spann <rupert.spann@ska.ac.za> wrote:
related to: pep-0584, PEP-0218, and ???
The request is that since dictionary keys can not be duplicated / are unique that they may behave the same as sets - and have the set operators actions and return sets. eg.:
aSet = set('a','b','c','bar') aDict = {'a':'aaa', 'b':'baa' , 'foo':fou' } bDict = {'a':'ea', 'b':'bee' , 'foo':fuh', 'bar': 'drink' }
aSet | aDict | bDict = set('a','b','c','foo','bar')
bDict & aDict =set('a','b','foo')
bDict - aSet =set('foo')
aDict^bDict =set('bar')
The dictionary class would support set filtering: New_dict_from_set = { your_key: old_dict[your_key] for your_key in aSET } e.g. a function such as: (taking the intersection) aDict.Subdictionary(aSet) ={'a':'aaa', 'b':'baa'}
set(dictionary) would return the Keys (as a mutable).
Adding an element to the mutable set would add a None as a value to the dictionary.
The "-=", "-", "+", "+=" would behave as specified in pep-0584 if both are dictionaries, but if one of the operands is a set - a set is returned.
The behaviour of bDict.Subdictionary(aDict) = {'a':'ea', 'b':'bee' , 'foo':fuh') i.e. treats aDict as a set
aDict.Subdictionary(List)== aDict.Subdictionary(Set(List))
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/YY3KZZ... Code of Conduct: http://python.org/psf/codeofconduct/
Hi Rupert Welcome to the list, if this is your first time here. You wrote:
aSet = set('a','b','c','bar')
Actually, Python isn't quite like that
aSet = set('a','b','c','bar') TypeError: set expected at most 1 arguments, got 4
I think you meant perhaps
aSet = set(['a','b','c','bar'])
Or I think better
set(['a','b','c','bar']) {'c', 'a', 'b', 'bar'}
I've been using Python for almost 20 years now, and often find myself producing little command line examples like this to check my memory regarding common types. Particularly string formatting and regular expressions. And particularly before posting to a public discussion list. -- Jonathan
On Tue, Sep 17, 2019 at 8:19 PM Rupert Spann <rupert.spann@ska.ac.za> wrote:
related to: pep-0584, PEP-0218, and ???
The request is that since dictionary keys can not be duplicated / are unique that they may behave the same as sets - and have the set operators actions and return sets.
This is a correct observation (as already mentioned by Anders). eg.:
aSet = set('a','b','c','bar') aDict = {'a':'aaa', 'b':'baa' , 'foo':fou' } bDict = {'a':'ea', 'b':'bee' , 'foo':fuh', 'bar': 'drink' }
aSet | aDict | bDict = set('a','b','c','foo','bar')
This can be already done by: aSet | set(aDict.keys()) | set(bDict.keys())
bDict & aDict =set('a','b','foo')
This a confusing way to write: set(bDict.keys()) | set(aDict.keys())
bDict - aSet =set('foo')
ditto.
aDict^bDict =set('bar')
The dictionary class would support set filtering: New_dict_from_set = { your_key: old_dict[your_key] for your_key in aSET } e.g. a function such as: (taking the intersection) aDict.Subdictionary(aSet) ={'a':'aaa', 'b':'baa'}
Filtering a dict (keys) by a set and producing a dict makes sense. (+1)
set(dictionary) would return the Keys (as a mutable).
This is again a confusing way to write: set(dictionary.keys())
Adding an element to the mutable set would add a None as a value to the dictionary.
Now I am not sure to follow it anymore. 'set' should return a set, adding anything to it cannot change the original dict, from which the keys were used. If it does not return a set, then it is not set() anymore.
The "-=", "-", "+", "+=" would behave as specified in pep-0584 if both are dictionaries, but if one of the operands is a set - a set is returned.
After seeing a bit of PEP-0584 I believe it faces some problems similar to your proposal -> the expected properties of those operators do not map correctly to the "dict space". E.g. '+' is not commutative, '-' what happens when the first operand is not "big enough" (ditto for '-='), etc.
The behaviour of bDict.Subdictionary(aDict) = {'a':'ea', 'b':'bee' , 'foo':fuh')
This is again a confusing way to write: bDict.Subdictionary(set(aDict.keys())
i.e. treats aDict as a set
I think it is not possible to treat dict (object) as a set on an abstraction level. Imagine two dicts: d1 = {"a": 1} d2 = {"a": 2} Which set operations could you define on those which would give a sensible result as a dict object (analogical to the set counterpart)? I believe that not so many. You can however define dict keys as sets and then use all set operations on them and then _interpret_ the results somehow as a dict while using the original d1 and d2 values. But the interpretation is not directly obvious from dicts alone, you have to add some other rules/logic for that. Richard
Am 17.09.19 um 17:03 schrieb Rupert Spann:
related to: pep-0584, PEP-0218, and ???
The request is that since dictionary keys can not be duplicated / are unique that they may behave the same as sets - and have the set operators actions and return sets. eg.:
aSet = set('a','b','c','bar') aDict = {'a':'aaa', 'b':'baa' , 'foo':fou' } bDict = {'a':'ea', 'b':'bee' , 'foo':fuh', 'bar': 'drink' }
aSet | aDict | bDict = set('a','b','c','foo','bar')
The key-view objects created with my_dict.keys() already support set operations. Your example: aSet = set(['a','b','c','bar']) aDict = {'a': 'aaa', 'b': 'baa' , 'foo': 'fou' } bDict = {'a': 'ea', 'b': 'bee' , 'foo': 'fuh', 'bar': 'drink' } already works with .keys():
aSet | aDict.keys() | bDict.keys() {'a', 'b', 'bar', 'c', 'foo'}
bDict & aDict =set('a','b','foo')
bDict.keys() & aDict.keys() {'a', 'b', 'foo'}
bDict - aSet =set('foo')
bDict.keys() - aSet {'foo'}
aDict^bDict =set('bar')
aDict.keys() ^ bDict.keys() {'bar'}
The dictionary class would support set filtering: New_dict_from_set = { your_key: old_dict[your_key] for your_key in aSET } e.g. a function such as: (taking the intersection) aDict.Subdictionary(aSet) ={'a':'aaa', 'b':'baa'}
Dou you mean?:
{k: v for k, v in aDict.items() if k in aSet} {'a': 'aaa', 'b': 'baa'}
set(dictionary) would return the Keys (as a mutable).
Doesn't it do this already?:
set(aDict) {'a', 'b', 'foo'}
Mike
participants (5)
-
Anders Hovmöller
-
Jonathan Fine
-
Mike Müller
-
Richard Musil
-
Rupert Spann