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
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/Y6WUOIFVKGWOUDEO6W37DFZJ3QQWOECJ/
Code of Conduct: http://python.org/psf/codeofconduct/