本文整理汇总了Python中itertools.html方法的典型用法代码示例。如果您正苦于以下问题:Python itertools.html方法的具体用法?Python itertools.html怎么用?Python itertools.html使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类itertools
的用法示例。
在下文中一共展示了itertools.html方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: unique_everseen
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import html [as 别名]
def unique_everseen(iterable, key=None):
"List unique elements, preserving order. Remember all elements ever seen."
# unique_everseen('AAAABBBCCDAABBB') --> A B C D
# unique_everseen('ABBCcAD', str.lower) --> A B C D
# straight from the docs, https://docs.python.org/3/library/itertools.html#itertools-recipes
seen = set()
seen_add = seen.add
if key is None:
for element in itertools.filterfalse(seen.__contains__, iterable):
seen_add(element)
yield element
else:
for element in iterable:
k = key(element)
if k not in seen:
seen_add(k)
yield element
示例2: sublists
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import html [as 别名]
def sublists(lst, min_elmts=0, max_elmts=None):
"""Build a list of all possible sublists of a given list. Restrictions
on the length of the sublists can be posed via the min_elmts and max_elmts
parameters.
All sublists
have will have at least min_elmts elements and not more than max_elmts
elements.
Parameters
----------
lst : list
Original list from which sublists are generated.
min_elmts : int
Lower bound for the length of sublists.
max_elmts : int or None
If int, then max_elmts are the upper bound for the length of sublists.
If None, sublists' length is not restricted. In this case the longest
sublist will be of the same length as the original list lst.
Returns
-------
result : list
A list of all sublists of lst fulfilling the length restrictions.
"""
if max_elmts is None:
max_elmts = len(lst)
# for the following see also the definition of powerset() in
# https://docs.python.org/dev/library/itertools.html#itertools-recipes
result = itertools.chain.from_iterable(
itertools.combinations(lst, sublist_len)
for sublist_len in range(min_elmts, max_elmts+1))
if type(result) != list:
result = list(result)
return result
示例3: split_by
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import html [as 别名]
def split_by(array: List[Any], group_size: int, filler: Any) -> List[List[Any]]:
"""
Group elements into list of size `group_size` and fill empty cells with
`filler`. Recipe from https://docs.python.org/3/library/itertools.html
"""
args = [iter(array)] * group_size
return list(map(list, zip_longest(*args, fillvalue=filler)))
示例4: all_equal
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import html [as 别名]
def all_equal(iterable):
"""Returns True if all the elements are equal to each other
https://docs.python.org/3/library/itertools.html#itertools-recipes
"""
g = itertools.groupby(iterable)
return next(g, True) and not next(g, False)
示例5: morph
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import html [as 别名]
def morph(model, zs, n_per_morph=10, loop=True, save=True, name="morph", outdir="."):
"""Plot frames of morph between zs (np.array of 2+ latent points)"""
assert len(zs) > 1, "Must specify at least two latent pts for morph!"
dim = int(model.architecture[0]**0.5) # assume square images
def pairwise(iterable):
"""s -> (s0,s1), (s1,s2), (s2, s3), ..."""
# via https://docs.python.org/dev/library/itertools.html
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
if loop:
zs = np.append(zs, zs[:1], 0)
all_xs = []
for z1, z2 in pairwise(zs):
zs_morph = np.array([np.linspace(start, end, n_per_morph)
# interpolate across every z dimension
for start, end in zip(z1, z2)]).T
xs_reconstructed = model.decode(zs_morph)
all_xs.extend(xs_reconstructed)
for i, x in enumerate(all_xs):
plt.figure(figsize = (5, 5))
plt.imshow(x.reshape([dim, dim]), cmap="Greys")
# axes off
ax = plt.gca()
ax.set_frame_on(False)
ax.set_xticks([])
ax.set_yticks([])
plt.axis("off")
# plt.show()
if save:
title = "{}_latent_{}_round_{}_{}.{}.png".format(
model.datetime, "_".join(map(str, model.architecture)),
model.step, name, i)
plt.savefig(os.path.join(outdir, title), bbox_inches="tight")
示例6: pairwise
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import html [as 别名]
def pairwise(iterable):
# //docs.python.org/dev/library/itertools.html#recipes
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
示例7: product
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import html [as 别名]
def product(*args, **kwds):
"""
Taken from http://docs.python.org/library/itertools.html#itertools.product
"""
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
pools = map(tuple, args) * kwds.get('repeat', 1)
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)
示例8: unique_everseen
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import html [as 别名]
def unique_everseen(iterable, filterfalse_=itertools.filterfalse):
"""Unique elements, preserving order."""
# Itertools recipes:
# https://docs.python.org/3/library/itertools.html#itertools-recipes
seen = set()
seen_add = seen.add
for element in filterfalse_(seen.__contains__, iterable):
seen_add(element)
yield element
示例9: powerset
# 需要导入模块: import itertools [as 别名]
# 或者: from itertools import html [as 别名]
def powerset(iterable):
"""Returns the powerset of the iterable.
See https://docs.python.org/3.6/library/itertools.html#itertools-recipes
powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)
Args:
iterable: An iterable to create the powerset.
"""
return itertools.chain.from_iterable(
itertools.combinations(iterable, r) for r in range(len(iterable)+1)
)