Dictionary Views & Set Operations — Working with dictionary view objects

微信扫一扫,分享到朋友圈

Dictionary Views & Set Operations — Working with dictionary view objects

The keys
, values
and items
from a dictionary can be accessed using the .keys()
, .values()
and .items()
methods. These methods return view objects
which provide a view on the source dictionary.

The view objects dict_keys
and dict_items
support set
-like operations (the latter only when all values are hashable) which can be used to combine and filter dictionary elements.

Keys

Dictionary keys are always
hashable, so set
operations are always available on the dict_keys
view object.

All keys ( set
union)

To get all keys
from multiple dictionaries, you can use the set
union.

>>> d1 = {'key1':'value1', 'key2':'value2', 'key3':3}
>>> d2 = {'key3':'value3-new', 'key5':'value5'}

>>> d1.keys() | d2.keys()
{'key5', 'key3', 'key2', 'key1'}  # this is a set, not a dict

You can use the same approach to combine dict_items
and merge dictionaries.

Keys in common ( set
intersection)

The keys
common to two dictionaries can be determined using set
intersection ( &
).

>>> d1 = {'key1':'value1', 'key2':'value2', 'key3':3}
>>> d2 = {'key3':'value3-new', 'key5':'value5'}

>>> d1.keys() & d2.keys()
{'key3'}    # this is a set, not a dictionary

You could use the resulting set
to filter your dictionary using a dictionary comprehension.

>>> {k:d1[k] for k in keys}
{'key2':'value2', 'key1':'value1'}

Unique keys ( set
difference)

To retrieve keys unique to a given dictionary, you can use set
difference ( -
). Keys from the right hand dict_keys
are removed from the left, resulting in a set
of the remaining keys.

>>> d1 = {'key1':'value1', 'key2':'value2', 'key3':3}
>>> d2 = {'key3':'value3-new', 'key5':'value5'}

>>> d1.keys() - d2.keys()
{'key1', 'key2'}

Unique keys from both ( set
symmetric difference)

If you want items unique to both
dictionaries, the set
symmetric difference ( ^
) returns this. The result is items unique to both the left and right hand of the comparison.

>>> d1 = {'key1':'value1', 'key2':'value2', 'key3':3}
>>> d2 = {'key3':'value3-new', 'key5':'value5'}

>>> d1.keys() ^ d2.keys()
{'key5', 'key2', 'key1'}

Items

If both the keys and
values of a dictionary are hashable, the dict_items
view will support set
-like operations.

If the values are not
hashable all of these2 operations will all raise a TypeError
.

Merge ( set
union)

You can use set
union
operations to merge two dictionaries.

>>> d1 = {'key1':'value1', 'key2':'value2', 'key3':3}
>>> d2 = {'key3':'value3-new', 'key5':'value5'}
>>> d3 = {'key4':'value4', 'key6':'value6'}

>>> d = dict(d1.items() | d2.items() | d3.items())
>>> d
{'key1':'value1', 'key2':'value2', 'key3':'value3-new', 'key5':'value5', 'key4':'value4', 'key6':'value6'}

Since it is quite common for dictionary values to not
be hashable, you will probably want to use one of the other approaches for merging dictionaries
instead.

Common entries ( set
intersection)

The items
common to two dictionaries can be determined using set
intersection ( &
). Both the key and
value must match — items are compared as (key, value)
tuples.

>>> d1 = {'key1':'value1', 'key2':'value2', 'key3':3}
>>> d2 = {'key1':'value1', 'key5':'value5'}

>>> d1.items() & d2.items()
{('key1', 'value1')}    # this is a set, not a dict

Unique entries ( set
difference)

To retrieve items unique to a given dictionary, you can use set
difference ( -
). Items from the right hand dict_keys
are removed from the left, resulting in a set
of the remaining item (key, value)
tuples.

>>> d1 = {'key1':'value1', 'key2':'value2', 'key3':3}
>>> d2 = {'key3':'value3-new', 'key5':'value5'}

>>> d1.items() - d2.items()
{('key3', 3), ('key2', 'value2'), ('key1', 'value1')}

Unique entries from both ( set
symmetric difference)

If you want items unique to both
dictionaries, the set
symmetric difference ( ^
) returns this. The result is item (key, value)
tuples unique to both the left and right hand of the comparison.

>>> d1 = {'key1':'value1', 'key2':'value2', 'key3':3}
>>> d2 = {'key3':'value3-new', 'key5':'value5'}

>>> d1.items() ^ d2.items()
{('key2', 'value2'), ('key5', 'value5'), ('key1', 'value1'), ('key3', 3), ('key3', 'value3-new')}

微信扫一扫,分享到朋友圈

Dictionary Views & Set Operations — Working with dictionary view objects

消息人士:特斯拉Model 3三季度产量已超过5.1万辆

上一篇

mpvue将vue项目转换为小程序

下一篇

你也可能喜欢

Dictionary Views & Set Operations — Working with dictionary view objects

长按储存图像,分享给朋友