本文整理汇总了Python中itertools.tee方法的典型用法代码示例。如果您正苦于以下问题:Python itertools.tee方法的具体用法?Python itertools.tee怎么用?Python itertools.tee使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类itertools
的用法示例。
在下文中一共展示了itertools.tee方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_peek
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import tee [as 别名]
def test_peek(self):
def give_gen():
for i in range(1, 5):
yield i
def give_empty_gen():
for i in range(1, 1):
yield i
obj = give_gen()
for i in range(1, 5):
num, new_obj = peek(obj)
obj, new_obj = itertools.tee(obj)
self.assertEqual(i, num)
ret_val = peek(obj)
obj = give_empty_gen()
ret_val_1 = peek(obj)
self.assertEqual(ret_val, None)
self.assertEqual(ret_val_1, None)
示例2: build_dataset
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import tee [as 别名]
def build_dataset(args):
print("Building dataset from : {}".format(args.input))
print("-> Building {} random splits".format(args.nb_splits))
nlp = spacy.load('en', create_pipeline=custom_pipeline)
gen_a,gen_b = itertools.tee(data_generator(args.input),2)
data = [(z["reviewerID"],z["asin"],tok,z["overall"]) for z,tok in zip(tqdm((z for z in gen_a),desc="reading file"),nlp.pipe((x["reviewText"] for x in gen_b), batch_size=1000000, n_threads=8))]
print(data[0])
shuffle(data)
splits = [randint(0,args.nb_splits-1) for _ in range(0,len(data))]
count = Counter(splits)
print("Split distribution is the following:")
print(count)
return {"data":data,"splits":splits,"rows":("user_id","item_id","review","rating")}
示例3: iterintervals
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import tee [as 别名]
def iterintervals(self, n=2):
"""Iterate over groups of `n` consecutive measurement points in the
time series.
"""
# tee the original iterator into n identical iterators
streams = itertools.tee(iter(self), n)
# advance the "cursor" on each iterator by an increasing
# offset, e.g. if n=3:
#
# [a, b, c, d, e, f, ..., w, x, y, z]
# first cursor --> *
# second cursor --> *
# third cursor --> *
for stream_index, stream in enumerate(streams):
for _ in range(stream_index):
next(stream)
# now, zip the offset streams back together to yield tuples,
# in the n=3 example it would yield:
# (a, b, c), (b, c, d), ..., (w, x, y), (x, y, z)
for intervals in zip(*streams):
yield intervals
示例4: sliding_window
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import tee [as 别名]
def sliding_window(n, seq):
"""
Copied from toolz
https://toolz.readthedocs.io/en/latest/_modules/toolz/itertoolz.html#sliding_window
A sequence of overlapping subsequences
>>> list(sliding_window(2, [1, 2, 3, 4]))
[(1, 2), (2, 3), (3, 4)]
This function creates a sliding window suitable for transformations like
sliding means / smoothing
>>> mean = lambda seq: float(sum(seq)) / len(seq)
>>> list(map(mean, sliding_window(2, [1, 2, 3, 4])))
[1.5, 2.5, 3.5]
"""
return zip(*(collections.deque(itertools.islice(it, i), 0) or it
for i, it in enumerate(itertools.tee(seq, n))))
示例5: follow_path
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import tee [as 别名]
def follow_path(self, path, types, call_scope):
"""
Follows a path like::
self.follow_path(iter(['Foo', 'bar']), [a_type], from_somewhere)
to follow a call like ``module.a_type.Foo.bar`` (in ``from_somewhere``).
"""
results_new = []
iter_paths = itertools.tee(path, len(types))
for i, typ in enumerate(types):
fp = self._follow_path(iter_paths[i], typ, call_scope)
if fp is not None:
results_new += fp
else:
# This means stop iteration.
return types
return results_new
示例6: unnest
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import tee [as 别名]
def unnest(item):
# type: (Any) -> Iterable[Any]
target = None # type: Optional[Iterable]
if isinstance(item, Iterable) and not isinstance(item, six.string_types):
item, target = itertools.tee(item, 2)
else:
target = item
if getattr(target, "__iter__", None):
for el in target:
if isinstance(el, Iterable) and not isinstance(el, six.string_types):
el, el_copy = itertools.tee(el, 2)
for sub in unnest(el_copy):
yield sub
else:
yield el
else:
yield target
示例7: __call__
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import tee [as 别名]
def __call__(self, jobs_or_statepoints):
"Evaluate the schema for the given state points."
s = dict()
iterators = itertools.tee(jobs_or_statepoints, len(self))
for key, it in zip(self, iterators):
values = []
keys = key.split('.')
for sp in it:
if not isinstance(sp, Mapping):
sp = sp.statepoint
v = sp[keys[0]]
for k in keys[1:]:
v = v[k]
values.append(v)
s[key] = _collect_by_type(values)
return ProjectSchema(s)
示例8: random_bipartition_iter
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import tee [as 别名]
def random_bipartition_iter(iterable, relative_size=.5, random_state=1):
"""random_bipartition_iter."""
size_iterable, iterable1, iterable2 = tee(iterable, 3)
size = iterator_size(size_iterable)
part1_ids, part2_ids = random_bipartition(
size, relative_size=relative_size, random_state=random_state)
part1_iterable = selection_iterator(iterable1, part1_ids)
part2_iterable = selection_iterator(iterable2, part2_ids)
return part1_iterable, part2_iterable
示例9: join_pre_processes
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import tee [as 别名]
def join_pre_processes(iterable, pre_processes=None, weights=None):
"""join_pre_processes."""
graphs_list = list()
assert(len(weights) == len(pre_processes)), 'Different lengths'
# NOTE: we have to duplicate the sequences iterator if we want to use
# different modifiers in parallel
iterables = tee(iterable, len(pre_processes))
for pre_process_item, iterable_item in zip(pre_processes, iterables):
graphs_list.append(pre_process_item(iterable_item))
return (graphs_list, weights)
示例10: auto_relabel
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import tee [as 别名]
def auto_relabel(graphs, n_clusters=16, **opts):
"""Label nodes with cluster id."""
graphs, graphs_ = tee(graphs)
label_list, vecs_list = auto_label(graphs_, n_clusters=n_clusters, **opts)
relabeled_graphs = []
for labels, vecs, orig_graph in zip(label_list, vecs_list, graphs):
graph = nx.Graph(orig_graph)
for label, vec, u in zip(labels, vecs, graph.nodes()):
graph.nodes[u]['label'] = label
graph.nodes[u]['vec'] = list(vec)
relabeled_graphs.append(graph)
return relabeled_graphs
示例11: shared_parent
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import tee [as 别名]
def shared_parent(self):
"""
Returns the folder object that is the shared parent (the root of
a shared folder hierarchy) or None if there is no shared parent.
"""
root = self
a, b = itertools.tee(reversed(self.breadcrumbs()))
next(b, None)
for folder, parent in itertools.zip_longest(a, b):
if folder.shared:
root = folder
if parent is None or not parent.shared:
break
return root
示例12: nsmallest
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import tee [as 别名]
def nsmallest(n, iterable, key=None):
"""Find the n smallest elements in a dataset.
Equivalent to: sorted(iterable, key=key)[:n]
"""
# Short-cut for n==1 is to use min() when len(iterable)>0
if n == 1:
it = iter(iterable)
head = list(islice(it, 1))
if not head:
return []
if key is None:
return [min(chain(head, it))]
return [min(chain(head, it), key=key)]
# When n>=size, it's faster to use sorted()
try:
size = len(iterable)
except (TypeError, AttributeError):
pass
else:
if n >= size:
return sorted(iterable, key=key)[:n]
# When key is none, use simpler decoration
if key is None:
it = zip(iterable, count()) # decorate
result = _nsmallest(n, it)
return [r[0] for r in result] # undecorate
# General case, slowest method
in1, in2 = tee(iterable)
it = zip(map(key, in1), count(), in2) # decorate
result = _nsmallest(n, it)
return [r[2] for r in result] # undecorate
示例13: nlargest
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import tee [as 别名]
def nlargest(n, iterable, key=None):
"""Find the n largest elements in a dataset.
Equivalent to: sorted(iterable, key=key, reverse=True)[:n]
"""
# Short-cut for n==1 is to use max() when len(iterable)>0
if n == 1:
it = iter(iterable)
head = list(islice(it, 1))
if not head:
return []
if key is None:
return [max(chain(head, it))]
return [max(chain(head, it), key=key)]
# When n>=size, it's faster to use sorted()
try:
size = len(iterable)
except (TypeError, AttributeError):
pass
else:
if n >= size:
return sorted(iterable, key=key, reverse=True)[:n]
# When key is none, use simpler decoration
if key is None:
it = zip(iterable, count(0,-1)) # decorate
result = _nlargest(n, it)
return [r[0] for r in result] # undecorate
# General case, slowest method
in1, in2 = tee(iterable)
it = zip(map(key, in1), count(0,-1), in2) # decorate
result = _nlargest(n, it)
return [r[2] for r in result] # undecorate
示例14: __iter__
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import tee [as 别名]
def __iter__(self) -> Iterator[Any]:
if self._computed:
yield from self._dataset
else:
iterable, self._iterable = tee(self._iterable)
yield from iterable
示例15: test_search
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import tee [as 别名]
def test_search(self):
actual = self.pclient.images.search("alpine", 25)
names, length = itertools.tee(actual)
for img in names:
self.assertIn("alpine", img.name)
self.assertTrue(0 < len(list(length)) <= 25)