本文整理匯總了Python中UserDict.DictMixin.iteritems方法的典型用法代碼示例。如果您正苦於以下問題:Python DictMixin.iteritems方法的具體用法?Python DictMixin.iteritems怎麽用?Python DictMixin.iteritems使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類UserDict.DictMixin
的用法示例。
在下文中一共展示了DictMixin.iteritems方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: elements
# 需要導入模塊: from UserDict import DictMixin [as 別名]
# 或者: from UserDict.DictMixin import iteritems [as 別名]
def elements(self):
'''Iterator over elements.
It repeats each element as many times as its count.
>>> c = Counter('ABCABC')
>>> sorted(c.elements())
['A', 'A', 'B', 'B', 'C', 'C']
If an element's count has been set to zero or is a negative
number, elements() will ignore it.
'''
for elem, count in self.iteritems():
for _ in repeat(None, count):
yield elem
# Override dict methods where the meaning changes for Counter objects.
示例2: iteritems
# 需要導入模塊: from UserDict import DictMixin [as 別名]
# 或者: from UserDict.DictMixin import iteritems [as 別名]
def iteritems(self, raw=False):
""" Iterate self items. """
for key in self:
yield key, self.__getitem__(key, raw=raw)
示例3: most_common
# 需要導入模塊: from UserDict import DictMixin [as 別名]
# 或者: from UserDict.DictMixin import iteritems [as 別名]
def most_common(self, n=None):
'''List the n most common elements and their counts.
The list goes from the most common to the least. If n is
None, then list all element counts.
>>> Counter('abracadabra').most_common(3)
[('a', 5), ('r', 2), ('b', 2)]
'''
if n is None:
return sorted(self.iteritems(), key=itemgetter(1),
reverse=True)
return nlargest(n, self.iteritems(), key=itemgetter(1))
示例4: update
# 需要導入模塊: from UserDict import DictMixin [as 別名]
# 或者: from UserDict.DictMixin import iteritems [as 別名]
def update(self, iterable=None, **kwds):
'''Like dict.update() but add counts instead of replacing them.
Source can be an iterable, a dictionary, or another Counter
instance.
>>> c = Counter('which')
>>> c.update('witch') # add elements from another iterable
>>> d = Counter('watch')
>>> c.update(d) # add elements from another counter
>>> c['h'] # four 'h' in which, witch, and watch
4
'''
if iterable is not None:
if hasattr(iterable, 'iteritems'):
if self:
self_get = self.get
for elem, count in iterable.iteritems():
self[elem] = self_get(elem, 0) + count
else:
# fast path when counter is empty
dict.update(self, iterable)
else:
self_get = self.get
for elem in iterable:
self[elem] = self_get(elem, 0) + 1
if kwds:
self.update(kwds)
示例5: kvquery
# 需要導入模塊: from UserDict import DictMixin [as 別名]
# 或者: from UserDict.DictMixin import iteritems [as 別名]
def kvquery(root, **kwargs):
'''kvquery provides a convinient way of finding widgets in an
application that uses the kv style language.
example:
lets say you have a .kv file with the following Rule:
<MovieWidget>:
BoxLayout:
Video:
kvid: 'video'
Label:
text: root.movie_title
Label:
text: root.movie_description
in your python code, you may want to get the reference to
Video widget nested inside the widget you have a handle to.
# video will be the first node that jas a 'kvid' property == 'video'
video = kvquery(movie, kvid='video').next()
#lets get all teh labels in a list
labels = list(kvquery(movie, __class__=Label))
:Parameters:
`root`: root of the tree to queried
this node and all decendants will be iterated by the
returned generator.
`**kwargs`: **kwargs, key/value pairs
The keys corrosponf to porperty names, and values to the
property values of the widget nodes being queried. If a node
has at least one attr such that (gettattr(node, key) == value)
is true; it will be included in the iteration.
'''
def _query(w):
'''iternal query function / predicate for tree query
'''
for k, v in kwargs.iteritems():
if (v == getattr(w, k, None)):
return True
return filter_tree(root, _query)