本文整理汇总了Python中toolz.compatibility.map函数的典型用法代码示例。如果您正苦于以下问题:Python map函数的具体用法?Python map怎么用?Python map使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了map函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pluck
def pluck(ind, seqs, default=no_default):
""" plucks an element or several elements from each item in a sequence.
``pluck`` maps ``itertoolz.get`` over a sequence and returns one or more
elements of each item in the sequence.
This is equivalent to running `map(curried.get(ind), seqs)`
``ind`` can be either a single string/index or a sequence of
strings/indices.
``seqs`` should be sequence containing sequences or dicts.
e.g.
>>> data = [{'id': 1, 'name': 'Cheese'}, {'id': 2, 'name': 'Pies'}]
>>> list(pluck('name', data))
['Cheese', 'Pies']
>>> list(pluck([0, 1], [[1, 2, 3], [4, 5, 7]]))
[(1, 2), (4, 5)]
See Also:
get
map
"""
if default is no_default:
if isinstance(ind, list):
return map(operator.itemgetter(*ind), seqs)
return map(operator.itemgetter(ind), seqs)
elif isinstance(ind, list):
return (tuple(_get(item, seq, default) for item in ind)
for seq in seqs)
return (_get(ind, seq, default) for seq in seqs)
示例2: test_EqualityHashKey_default_key
def test_EqualityHashKey_default_key():
EqualityHashDefault = curry(EqualityHashKey, None)
L1 = [1]
L2 = [2]
data1 = [L1, L1, L2, [], [], [1], [2], {}, ()]
set1 = set(map(EqualityHashDefault, data1))
set2 = set(map(EqualityHashDefault, [[], [1], [2], {}, ()]))
assert set1 == set2
assert len(set1) == 5
# Test that ``EqualityHashDefault(item)`` is distinct from ``item``
T0 = ()
T1 = (1,)
data2 = list(map(EqualityHashDefault, [T0, T0, T1, T1, (), (1,)]))
data2.extend([T0, T1, (), (1,)])
set3 = set(data2)
assert set3 == set([(), (1,), EqualityHashDefault(()),
EqualityHashDefault((1,))])
assert len(set3) == 4
assert EqualityHashDefault(()) in set3
assert EqualityHashDefault((1,)) in set3
# Miscellaneous
E1 = EqualityHashDefault(L1)
E2 = EqualityHashDefault(L2)
assert str(E1) == '=[1]='
assert repr(E1) == '=[1]='
assert E1 != E2
assert not (E1 == E2)
assert E1 == EqualityHashDefault(L1)
assert not (E1 != EqualityHashDefault(L1))
assert E1 != L1
assert not (E1 == L1)
示例3: fold
def fold(binop, seq, default=no_default, map=map, chunksize=128, combine=None):
"""
Reduce without guarantee of ordered reduction.
inputs:
``binop`` - associative operator. The associative property allows us to
leverage a parallel map to perform reductions in parallel.
``seq`` - a sequence to be aggregated
``default`` - an identity element like 0 for ``add`` or 1 for mul
``map`` - an implementation of ``map``. This may be parallel and
determines how work is distributed.
``chunksize`` - Number of elements of ``seq`` that should be handled
within a single function call
``combine`` - Binary operator to combine two intermediate results.
If ``binop`` is of type (total, item) -> total
then ``combine`` is of type (total, total) -> total
Defaults to ``binop`` for common case of operators like add
Fold chunks up the collection into blocks of size ``chunksize`` and then
feeds each of these to calls to ``reduce``. This work is distributed
with a call to ``map``, gathered back and then refolded to finish the
computation. In this way ``fold`` specifies only how to chunk up data but
leaves the distribution of this work to an externally provided ``map``
function. This function can be sequential or rely on multithreading,
multiprocessing, or even distributed solutions.
If ``map`` intends to serialize functions it should be prepared to accept
and serialize lambdas. Note that the standard ``pickle`` module fails
here.
Example
-------
>>> # Provide a parallel map to accomplish a parallel sum
>>> from operator import add
>>> fold(add, [1, 2, 3, 4], chunksize=2, map=map)
10
"""
if combine is None:
combine = binop
chunks = partition_all(chunksize, seq)
# Evaluate sequence in chunks via map
if default is no_default:
results = map(lambda chunk: reduce(binop, chunk), chunks)
else:
results = map(lambda chunk: reduce(binop, chunk, default), chunks)
results = list(results) # TODO: Support complete laziness
if len(results) == 1: # Return completed result
return results[0]
else: # Recurse to reaggregate intermediate results
return fold(combine, results, map=map, chunksize=chunksize)
示例4: test_load_from_dir_of_jsonlines
def test_load_from_dir_of_jsonlines(ctx):
dfs = []
dfc = df.copy()
for i in range(3):
dfc['id'] += i
dfs.append(dfc.copy())
expected = pd.concat(dfs, axis=0, ignore_index=True)
with jslines() as d:
result = odo(Directory(JSONLines)(d), ctx)
assert (set(map(frozenset, odo(result, list))) ==
set(map(frozenset, odo(expected, list))))
示例5: multihash
def multihash(x):
try:
return hash(x)
except TypeError:
if isinstance(x, (list, tuple, set, frozenset)):
return hash(tuple(map(multihash, x)))
if type(x) is dict:
return hash(frozenset(map(multihash, x.items())))
if type(x) is slice:
return hash((x.start, x.stop, x.step))
raise TypeError("Hashing not covered for " + str(x))
示例6: _to_lists
def _to_lists(seq, n=10):
"""iter of iters -> finite list of finite lists
"""
def initial(s):
return list(take(n, s))
return initial(map(initial, seq))
示例7: merge_sorted
def merge_sorted(*iters, **kwargs):
""" Merge and sort a collection of sorted collections
>>> list(merge_sorted([1, 3, 5], [2, 4, 6]))
[1, 2, 3, 4, 5, 6]
>>> ''.join(merge_sorted('abc', 'abc', 'abc'))
'aaabbbccc'
"""
key = kwargs.get('key', identity)
iters = map(iter, iters)
pq = Queue.PriorityQueue()
def inject_first_element(it, tiebreaker=None):
try:
item = next(it)
pq.put((key(item), item, tiebreaker, it))
except StopIteration:
pass
# Initial population
for i, it in enumerate(iters):
inject_first_element(it, i)
# Repeatedly yield and then repopulate from the same iterator
while not pq.empty():
_, item, tb, it = pq.get()
yield item
inject_first_element(it, tb)
示例8: mapcat
def mapcat(func, seqs):
""" Apply func to each sequence in seqs, concatenating results.
>>> list(mapcat(lambda s: [c.upper() for c in s],
... [["a", "b"], ["c", "d", "e"]]))
['A', 'B', 'C', 'D', 'E']
"""
return concat(map(func, seqs))
示例9: merge_sorted
def merge_sorted(*seqs, **kwargs):
""" Merge and sort a collection of sorted collections
This works lazily and only keeps one value from each iterable in memory.
>>> list(merge_sorted([1, 3, 5], [2, 4, 6]))
[1, 2, 3, 4, 5, 6]
>>> ''.join(merge_sorted('abc', 'abc', 'abc'))
'aaabbbccc'
The "key" function used to sort the input may be passed as a keyword.
>>> list(merge_sorted([2, 3], [1, 3], key=lambda x: x // 3))
[2, 1, 3, 3]
"""
key = kwargs.get('key', None)
if key is None:
# heapq.merge does what we do below except by val instead of key(val)
for item in heapq.merge(*seqs):
yield item
else:
# The commented code below shows an alternative (slower) implementation
# to apply a key function for sorting.
#
# mapper = lambda i, item: (key(item), i, item)
# keyiters = [map(partial(mapper, i), itr) for i, itr in
# enumerate(seqs)]
# return (item for (item_key, i, item) in heapq.merge(*keyiters))
# binary heap as a priority queue
pq = []
# Initial population
for itnum, it in enumerate(map(iter, seqs)):
try:
item = next(it)
pq.append([key(item), itnum, item, it])
except StopIteration:
pass
heapq.heapify(pq)
# Repeatedly yield and then repopulate from the same iterator
while True:
try:
while True:
# raises IndexError when pq is empty
_, itnum, item, it = s = pq[0]
yield item
item = next(it) # raises StopIteration when exhausted
s[0] = key(item)
s[2] = item
heapq.heapreplace(pq, s) # restore heap condition
except StopIteration:
heapq.heappop(pq) # remove empty iterator
except IndexError:
return
示例10: pprint
def pprint(g):
""" Pretty print a tree of goals """
if callable(g) and hasattr(g, "__name__"):
return g.__name__
if isinstance(g, type): # pragma: no cover
return g.__name__
if isinstance(g, tuple):
return "(" + ", ".join(map(pprint, g)) + ")"
return str(g)
示例11: pprint
def pprint(g):
""" Pretty print a tree of goals """
if callable(g) and hasattr(g, '__name__'):
return g.__name__
if isinstance(g, type):
return g.__name__
if isinstance(g, tuple):
return "(" + ', '.join(map(pprint, g)) + ")"
return str(g)
示例12: freeze
def freeze(d):
""" Freeze container to hashable form
>>> freeze(1)
1
>>> freeze([1, 2])
(1, 2)
>>> freeze({1: 2}) # doctest: +SKIP
frozenset([(1, 2)])
"""
if isinstance(d, dict):
return frozenset(map(freeze, d.items()))
if isinstance(d, set):
return frozenset(map(freeze, d))
if isinstance(d, (tuple, list)):
return tuple(map(freeze, d))
return d
示例13: interleave
def interleave(seqs, pass_exceptions=()):
iters = map(iter, seqs)
while iters:
newiters = []
for itr in iters:
try:
yield next(itr)
newiters.append(itr)
except (StopIteration,) + tuple(pass_exceptions):
pass
iters = newiters
示例14: valmap
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))))
示例15: keymap
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)))