python学习笔记整理——dictView [未整理]

Dictionary view objects简介

The objects returned by dict.viewkeys(), dict.viewvalues() and dict.viewitems() are view ob-
jects. They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the
view reflects these changes.
Dictionary views can be iterated over to yield their respective data, and support membership tests:

len(dictview)

Return the number of entries in the dictionary.

iter(dictview)

Return an iterator over the keys, values or items (represented as tuples of (key, value)) in the dictionary.
Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementa-
tions, and depends on the dictionary’s history of insertions and deletions. If keys, values and items views are
iterated over with no intervening modifications to the dictionary, the order of items will directly correspond. This
allows the creation of (value, key) pairs using zip(): pairs = zip(d.values(), d.keys()).
Another way to create the same list is pairs = [(v, k) for (k, v) in d.items()].
Iterating views while adding or deleting entries in the dictionary may raise a RuntimeError or fail to iterate
over all entries.

x in dictview

Return True if x is in the underlying dictionary’s keys, values or items (in the latter case, x should be a (key,
value) tuple).
Keys views are set-like since their entries are unique and hashable. If all values are hashable, so that (key, value) pairs
are unique and hashable, then the items view is also set-like. (Values views are not treated as set-like since the entries
are generally not unique.) Then these set operations are available (“other” refers either to another view or a set):

dictview & other

Return the intersection of the dictview and the other object as a new set.

dictview | other

Return the union of the dictview and the other object as a new set.

dictview - other

Return the difference between the dictview and the other object (all elements in dictview that aren’t in other) as
a new set.

dictview ^ other

Return the symmetric difference (all elements either in dictview or other, but not in both) of the dictview and
the other object as a new set.

An example of dictionary view usage:
          >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
          >>> keys = dishes.viewkeys()
          >>> values = dishes.viewvalues()
          >>> # iteration
          >>> n = 0
          >>> for val in values:
          ... n += val
          >>> print(n)
          504
          >>> # keys and values are iterated over in the same order
          >>> list(keys)
          ['eggs', 'bacon', 'sausage', 'spam']
          >>> list(values)
          [2, 1, 1, 500]
          >>> # view objects are dynamic and reflect dict changes
          >>> del dishes['eggs']
          >>> del dishes['sausage']
          >>> list(keys)
          ['spam', 'bacon']
          >>> # set operations
          >>> keys & {'eggs', 'bacon', 'salad'}
          {'bacon'}

转载于:https://www.cnblogs.com/learn-to-rock/p/5315116.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值