本文整理汇总了Python中toolz.compatibility.iteritems函数的典型用法代码示例。如果您正苦于以下问题:Python iteritems函数的具体用法?Python iteritems怎么用?Python iteritems使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了iteritems函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: merge_with
def merge_with(func, *dicts):
""" Merge dictionaries and apply function to combined values
A key may occur in more than one dict, and all values mapped from the key
will be passed to the function as a list, such as func([val1, val2, ...]).
>>> merge_with(sum, {1: 1, 2: 2}, {1: 10, 2: 20})
{1: 11, 2: 22}
>>> merge_with(first, {1: 1, 2: 2}, {2: 20, 3: 30}) # doctest: +SKIP
{1: 1, 2: 2, 3: 30}
See Also:
merge
"""
if len(dicts) == 1 and not isinstance(dicts[0], dict):
dicts = dicts[0]
result = {}
for d in dicts:
for k, v in iteritems(d):
if k not in result:
result[k] = [v]
else:
result[k].append(v)
return dict((k, func(v)) for k, v in iteritems(result))
示例2: test_dict_iteration
def test_dict_iteration():
d = {'a': 1, 'b': 2, 'c': 3}
assert not isinstance(iteritems(d), list)
assert not isinstance(iterkeys(d), list)
assert not isinstance(itervalues(d), list)
assert set(iteritems(d)) == set(d.items())
assert set(iterkeys(d)) == set(d.keys())
assert set(itervalues(d)) == set(d.values())
示例3: groupby
def groupby(key, seq):
""" Group a collection by a key function
>>> names = ['Alice', 'Bob', 'Charlie', 'Dan', 'Edith', 'Frank']
>>> groupby(len, names) # doctest: +SKIP
{3: ['Bob', 'Dan'], 5: ['Alice', 'Edith', 'Frank'], 7: ['Charlie']}
>>> iseven = lambda x: x % 2 == 0
>>> groupby(iseven, [1, 2, 3, 4, 5, 6, 7, 8]) # doctest: +SKIP
{False: [1, 3, 5, 7], True: [2, 4, 6, 8]}
Non-callable keys imply grouping on a member.
>>> groupby('gender', [{'name': 'Alice', 'gender': 'F'},
... {'name': 'Bob', 'gender': 'M'},
... {'name': 'Charlie', 'gender': 'M'}]) # doctest:+SKIP
{'F': [{'gender': 'F', 'name': 'Alice'}],
'M': [{'gender': 'M', 'name': 'Bob'},
{'gender': 'M', 'name': 'Charlie'}]}
See Also:
countby
"""
if not callable(key):
key = getter(key)
d = collections.defaultdict(lambda: [].append)
for item in seq:
d[key(item)](item)
rv = {}
for k, v in iteritems(d):
rv[k] = v.__self__
return rv
示例4: merge_frequencies
def merge_frequencies(seqs):
first, rest = seqs[0], seqs[1:]
if not rest:
return first
out = defaultdict(int)
out.update(first)
for d in rest:
for k, v in iteritems(d):
out[k] += v
return out
示例5: _unify
def _unify(u, v, s):
if len(u) != len(v):
return False
for key, uval in iteritems(u):
if key not in v:
return False
s = unify(uval, v[key], s)
if s is False:
return False
return s
示例6: itemmap
def itemmap(func, d):
""" Apply function to items of dictionary
>>> accountids = {"Alice": 10, "Bob": 20}
>>> itemmap(reversed, accountids) # doctest: +SKIP
{10: "Alice", 20: "Bob"}
See Also:
keymap
valmap
"""
return dict(map(func, iteritems(d)))
示例7: itemmap
def itemmap(func, d, factory=dict):
""" Apply function to items of dictionary
>>> accountids = {"Alice": 10, "Bob": 20}
>>> itemmap(reversed, accountids) # doctest: +SKIP
{10: "Alice", 20: "Bob"}
See Also:
keymap
valmap
"""
rv = factory()
rv.update(map(func, iteritems(d)))
return rv
示例8: valfilter
def valfilter(predicate, d):
""" Filter items in dictionary by value
>>> iseven = lambda x: x % 2 == 0
>>> d = {1: 2, 2: 3, 3: 4, 4: 5}
>>> valfilter(iseven, d)
{1: 2, 3: 4}
See Also:
keyfilter
valmap
"""
rv = {}
for k, v in iteritems(d):
if predicate(v):
rv[k] = v
return rv
示例9: keyfilter
def keyfilter(predicate, d):
""" Filter items in dictionary by key
>>> iseven = lambda x: x % 2 == 0
>>> d = {1: 2, 2: 3, 3: 4, 4: 5}
>>> keyfilter(iseven, d)
{2: 3, 4: 5}
See Also:
valfilter
keymap
"""
rv = {}
for k, v in iteritems(d):
if predicate(k):
rv[k] = v
return rv
示例10: unpack_kwargs
def unpack_kwargs(kwargs):
""" Extracts dask values from kwargs
Currently only dask.bag.Item and python literal values are supported.
Returns a merged dask graph and a list of [key, val] pairs suitable for
eventually constructing a dict.
"""
dsk = {}
kw_pairs = []
for key, val in iteritems(kwargs):
if isinstance(val, Item):
dsk.update(val.dask)
val = val.key
# TODO elif isinstance(val, Value):
elif isinstance(val, Base):
raise NotImplementedError(
'%s not supported as kwarg value to Bag.map_partitions'
% type(val).__name__)
kw_pairs.append([key, val])
return dsk, kw_pairs
示例11: groupby
def groupby(func, seq):
""" Group a collection by a key function
>>> names = ['Alice', 'Bob', 'Charlie', 'Dan', 'Edith', 'Frank']
>>> groupby(len, names)
{3: ['Bob', 'Dan'], 5: ['Alice', 'Edith', 'Frank'], 7: ['Charlie']}
>>> iseven = lambda x: x % 2 == 0
>>> groupby(iseven, [1, 2, 3, 4, 5, 6, 7, 8])
{False: [1, 3, 5, 7], True: [2, 4, 6, 8]}
See Also:
countby
"""
d = collections.defaultdict(lambda: [].append)
for item in seq:
d[func(item)](item)
rv = {}
for k, v in iteritems(d):
rv[k] = v.__self__
return rv
示例12: itemfilter
def itemfilter(predicate, d, factory=dict):
""" Filter items in dictionary by item
>>> def isvalid(item):
... k, v = item
... return k % 2 == 0 and v < 4
>>> d = {1: 2, 2: 3, 3: 4, 4: 5}
>>> itemfilter(isvalid, d)
{2: 3}
See Also:
keyfilter
valfilter
itemmap
"""
rv = factory()
for item in iteritems(d):
if predicate(item):
k, v = item
rv[k] = v
return rv