本文整理匯總了Python中itertools.starmap方法的典型用法代碼示例。如果您正苦於以下問題:Python itertools.starmap方法的具體用法?Python itertools.starmap怎麽用?Python itertools.starmap使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類itertools
的用法示例。
在下文中一共展示了itertools.starmap方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: elements
# 需要導入模塊: import itertools [as 別名]
# 或者: from itertools import starmap [as 別名]
def elements(self):
'''Iterator over elements repeating each as many times as its count.
>>> c = Counter('ABCABC')
>>> sorted(c.elements())
['A', 'A', 'B', 'B', 'C', 'C']
# Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1
>>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
>>> product = 1
>>> for factor in prime_factors.elements(): # loop over factors
... product *= factor # and multiply them
>>> product
1836
Note, if an element's count has been set to zero or is a negative
number, elements() will ignore it.
'''
# Emulate Bag.do from Smalltalk and Multiset.begin from C++.
return _chain.from_iterable(_starmap(_repeat, self.items()))
# Override dict methods where necessary
示例2: _map_async
# 需要導入模塊: import itertools [as 別名]
# 或者: from itertools import starmap [as 別名]
def _map_async(self, func, iterable, mapper, chunksize=None, callback=None,
error_callback=None):
'''
Helper function to implement map, starmap and their async counterparts.
'''
if self._state != RUN:
raise ValueError("Pool not running")
if not hasattr(iterable, '__len__'):
iterable = list(iterable)
if chunksize is None:
chunksize, extra = divmod(len(iterable), len(self._pool) * 4)
if extra:
chunksize += 1
if len(iterable) == 0:
chunksize = 0
task_batches = Pool._get_tasks(func, iterable, chunksize)
result = MapResult(self._cache, chunksize, len(iterable), callback,
error_callback=error_callback)
self._taskqueue.put((((result._job, i, mapper, (x,), {})
for i, x in enumerate(task_batches)), None))
return result
示例3: individuals_equal
# 需要導入模塊: import itertools [as 別名]
# 或者: from itertools import starmap [as 別名]
def individuals_equal(self, other):
return (
self.num_individuals == other.num_individuals
and np.allclose(
self.individuals_time[:], other.individuals_time[:], equal_nan=True
)
and all(
itertools.starmap(
np.array_equal,
zip(self.individuals_metadata[:], other.individuals_metadata[:]),
)
)
and all(
itertools.starmap(
np.array_equal,
zip(self.individuals_location[:], other.individuals_location[:]),
)
)
)
示例4: sites_equal
# 需要導入模塊: import itertools [as 別名]
# 或者: from itertools import starmap [as 別名]
def sites_equal(self, other):
return (
self.num_sites == other.num_sites
and self.num_inference_sites == other.num_inference_sites
and np.all(self.sites_position[:] == other.sites_position[:])
and np.all(self.sites_inference[:] == other.sites_inference[:])
and np.all(self.sites_genotypes[:] == other.sites_genotypes[:])
and np.allclose(self.sites_time[:], other.sites_time[:], equal_nan=True)
and all(
itertools.starmap(
np.array_equal, zip(self.sites_metadata[:], other.sites_metadata[:])
)
)
and all(
itertools.starmap(
np.array_equal, zip(self.sites_alleles[:], other.sites_alleles[:])
)
)
)
示例5: elements
# 需要導入模塊: import itertools [as 別名]
# 或者: from itertools import starmap [as 別名]
def elements(self):
'''Iterator over elements repeating each as many times as its count.
>>> c = Counter('ABCABC')
>>> sorted(c.elements())
['A', 'A', 'B', 'B', 'C', 'C']
# Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1
>>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
>>> product = 1
>>> for factor in prime_factors.elements(): # loop over factors
... product *= factor # and multiply them
>>> product
1836
Note, if an element's count has been set to zero or is a negative
number, elements() will ignore it.
'''
# Emulate Bag.do from Smalltalk and Multiset.begin from C++.
return _chain.from_iterable(_starmap(_repeat, self.iteritems()))
# Override dict methods where necessary
示例6: test_timedelta64_equal_timedelta_supported_ops
# 需要導入模塊: import itertools [as 別名]
# 或者: from itertools import starmap [as 別名]
def test_timedelta64_equal_timedelta_supported_ops(self, op):
ser = Series([Timestamp('20130301'), Timestamp('20130228 23:00:00'),
Timestamp('20130228 22:00:00'),
Timestamp('20130228 21:00:00')])
intervals = 'D', 'h', 'm', 's', 'us'
# TODO: unused
# npy16_mappings = {'D': 24 * 60 * 60 * 1000000,
# 'h': 60 * 60 * 1000000,
# 'm': 60 * 1000000,
# 's': 1000000,
# 'us': 1}
def timedelta64(*args):
return sum(starmap(np.timedelta64, zip(args, intervals)))
for d, h, m, s, us in product(*([range(2)] * 5)):
nptd = timedelta64(d, h, m, s, us)
pytd = timedelta(days=d, hours=h, minutes=m, seconds=s,
microseconds=us)
lhs = op(ser, nptd)
rhs = op(ser, pytd)
assert_series_equal(lhs, rhs)
示例7: minimize_class_set_by_most_generic
# 需要導入模塊: import itertools [as 別名]
# 或者: from itertools import starmap [as 別名]
def minimize_class_set_by_most_generic(
schema: s_schema.Schema,
classes: Iterable[so.InheritingObjectT]
) -> List[so.InheritingObjectT]:
"""Minimize the given set of objects by filtering out all subclasses."""
classes = list(classes)
mros = [set(p.get_ancestors(schema).objects(schema)) for p in classes]
count = len(classes)
smap = itertools.starmap
# Return only those entries that do not have other entries in their mro
result = [
scls for i, scls in enumerate(classes)
if not any(smap(set.__contains__,
((mros[i], classes[j])
for j in range(count) if j != i)))
]
return result
示例8: minimize_class_set_by_least_generic
# 需要導入模塊: import itertools [as 別名]
# 或者: from itertools import starmap [as 別名]
def minimize_class_set_by_least_generic(
schema: s_schema.Schema,
classes: Iterable[so.InheritingObjectT]
) -> List[so.InheritingObjectT]:
"""Minimize the given set of objects by filtering out all superclasses."""
classes = list(classes)
mros = [set(p.get_ancestors(schema).objects(schema)) | {p}
for p in classes]
count = len(classes)
smap = itertools.starmap
# Return only those entries that are not present in other entries' mro
result = [
scls for i, scls in enumerate(classes)
if not any(smap(set.__contains__,
((mros[j], classes[i])
for j in range(count) if j != i)))
]
return result
示例9: maximal
# 需要導入模塊: import itertools [as 別名]
# 或者: from itertools import starmap [as 別名]
def maximal(iterable, comparison=operator.lt, _groupkey=operator.itemgetter(0)):
"""Yield the unique maximal elements from ``iterable`` using ``comparison``.
>>> list(maximal([1, 2, 3, 3]))
[3]
>>> list(maximal([1]))
[1]
"""
iterable = set(iterable)
if len(iterable) < 2:
return iter(iterable)
return (item
for item, pairs in groupby(permutations(iterable, 2), key=_groupkey)
if not any(starmap(comparison, pairs)))
示例10: _promote_numeric_binop
# 需要導入模塊: import itertools [as 別名]
# 或者: from itertools import starmap [as 別名]
def _promote_numeric_binop(exprs, op):
bounds, dtypes = [], []
for arg in exprs:
dtypes.append(arg.type())
if hasattr(arg.op(), 'value'):
# arg.op() is a literal
bounds.append([arg.op().value])
else:
bounds.append(arg.type().bounds)
# In some cases, the bounding type might be int8, even though neither
# of the types are that small. We want to ensure the containing type is
# _at least_ as large as the smallest type in the expression.
values = starmap(op, product(*bounds))
dtypes += [dt.infer(value, allow_overflow=True) for value in values]
return dt.highest_precedence(dtypes)
示例11: format_meters
# 需要導入模塊: import itertools [as 別名]
# 或者: from itertools import starmap [as 別名]
def format_meters(caption, meters_kv, kv_format, glue):
log_str = [caption]
log_str.extend(itertools.starmap(kv_format.format, sorted(meters_kv.items())))
return glue.join(log_str)
示例12: sequencial_map
# 需要導入模塊: import itertools [as 別名]
# 或者: from itertools import starmap [as 別名]
def sequencial_map(fn, list_of_args):
return list(itertools.starmap(fn, list_of_args))
示例13: test_parallel_map_one
# 需要導入模塊: import itertools [as 別名]
# 或者: from itertools import starmap [as 別名]
def test_parallel_map_one(self):
utils.parallel_map.MAX_WORKERS = 1
starmap = itertools.starmap
with mock.patch("itertools.starmap") as sm:
sm.side_effect = starmap
self.assertEqual([1, 2, 3],
utils.parallel_map(lambda x: x,
[[1], [2], [3]]))
sm.assert_called()
示例14: test_parallel_map_four
# 需要導入模塊: import itertools [as 別名]
# 或者: from itertools import starmap [as 別名]
def test_parallel_map_four(self):
utils.parallel_map.MAX_WORKERS = 4
starmap = itertools.starmap
with mock.patch("itertools.starmap") as sm:
sm.side_effect = starmap
self.assertEqual([1, 2, 3],
utils.parallel_map(lambda x: x,
[[1], [2], [3]]))
sm.assert_not_called()
示例15: starmapstar
# 需要導入模塊: import itertools [as 別名]
# 或者: from itertools import starmap [as 別名]
def starmapstar(args):
return list(itertools.starmap(args[0], args[1]))
#
# Code run by worker processes
#