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())
def keymap(func, d): """ Apply function to keys of dictionary >>> bills = {"Alice": [20, 15, 30], "Bob": [10, 35]} >>> keymap(str.lower, bills) # doctest: +SKIP {'alice': [20, 15, 30], 'bob': [10, 35]} See Also: valmap """ return dict(zip(map(func, iterkeys(d)), itervalues(d)))
def valmap(func, d): """ Apply function to values of dictionary >>> bills = {"Alice": [20, 15, 30], "Bob": [10, 35]} >>> valmap(sum, bills) # doctest: +SKIP {'Alice': 65, 'Bob': 45} See Also: keymap """ return dict(zip(iterkeys(d), map(func, itervalues(d))))
def pkeymap(f, d, factory=dict, threads=None, timeout=None): """ Apply `f` to each key of dictionary `d` and return a new dictionary. Blocks until entire dictionary can be realized. Takes a `factory` to allow a return type that implements `collections.MutableMapping` other than `dict`. """ rv = factory() rv.update(zip(pmap(f, iterkeys(d), threads, timeout), itervalues(d))) return rv
def keymap(func, d, factory=dict): """ Apply function to keys of dictionary >>> bills = {"Alice": [20, 15, 30], "Bob": [10, 35]} >>> keymap(str.lower, bills) # doctest: +SKIP {'alice': [20, 15, 30], 'bob': [10, 35]} See Also: valmap itemmap """ rv = factory() rv.update(zip(map(func, iterkeys(d)), itervalues(d))) return rv
def valmap(func, d, factory=dict): """ Apply function to values of dictionary >>> bills = {"Alice": [20, 15, 30], "Bob": [10, 35]} >>> valmap(sum, bills) # doctest: +SKIP {'Alice': 65, 'Bob': 45} See Also: keymap itemmap """ rv = factory() rv.update(zip(iterkeys(d), map(func, itervalues(d)))) return rv