gpt4 book ai didi

python - 如何获取字符串中所有字符的 zip。 zip 遗漏了最后一个字符,并且 itertools.zip_longest 不添加任何字符

转载 作者:行者123 更新时间:2023-12-01 03:54:31 25 4
gpt4 key购买 nike

我将 itertools.zip_longest 的结果传递给 itertools.product,但是当它到达末尾并发现 None 时,我收到错误。

我得到的错误是:错误:(, TypeError('序列项 0: 预期的 str 实例,未找到 NoneType',), )

如果我使用 zip 而不是 itertools.zip_longest 那么我不会得到所有项目。

这是我用来生成 zip 的代码:

def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
print(args)
#return zip(*args)
return itertools.zip_longest(*args)

sCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~`!@#$%^&*()_-+={[}]|\"""':;?/>.<,"

for x in grouper(sCharacters, 4):
print(x)

这是输出。第一个是 itertools.zip_longest,第二个是 zip。您可以看到第一个带有 None 项目,第二个缺少最后一个项目,即逗号:','

Python generator that groups another iterable into groups of N [duplicate]

如何获得字符串中所有个字符的压缩,而末尾没有字符。或者如何避免这个错误?

感谢您的宝贵时间。

最佳答案

我之前必须在性能关键的情况下解决这个问题,所以这是我找到的最快的代码(无论iterable中的值如何):

from itertools import zip_longest

def grouper(n, iterable):
fillvalue = object() # Guaranteed unique sentinel, cannot exist in iterable
for tup in zip_longest(*(iter(iterable),) * n, fillvalue=fillvalue):
if tup[-1] is fillvalue:
yield tuple(v for v in tup if v is not fillvalue)
else:
yield tup

据我所知,当输入足够长并且 block 大小足够小时,上面的内容是无与伦比的。对于 block 大小相当大的情况,它可能会输给这种更丑陋的情况,但通常不会太多:

from future_builtins import map  # Only on Py2, and required there
from itertools import islice, repeat, starmap, takewhile
from operator import truth # Faster than bool when guaranteed non-empty call

def grouper(n, iterable):
'''Returns a generator yielding n sized groups from iterable

For iterables not evenly divisible by n, the final group will be undersized.
'''
# Can add tests to special case other types if you like, or just
# use tuple unconditionally to match `zip`
rettype = ''.join if type(iterable) is str else tuple

# Keep islicing n items and converting to groups until we hit an empty slice
return takewhile(truth, map(rettype, starmap(islice, repeat((iter(iterable), n)))))

如果没有足够的项目来完成该组,这两种方法都会无缝地使最终元素不完整。它运行得非常快,因为实际上所有工作在“设置”后都被推送到 CPython 中的 C 层,因此无论迭代有多长,Python 级别的工作都是相同的,只有 C 级别的工作增加。也就是说,它做了很多的C工作,这就是为什么zip_longest解决方案(它做的C工作要少得多,除了最终的之外,只进行微不足道的Python级别的工作) block )通常会打败它。

与选项 #2 相比,速度较慢但更易读的等效代码(但跳过动态返回类型而只使用 tuple)是:

 def grouper(n, iterable):
iterable = iter(iterable)
while True:
x = tuple(islice(iterable, n))
if not x:
return
yield x

或更简洁地使用 Python 3.8+ 的海象运算符:

 def grouper(n, iterable):
iterable = iter(iterable)
while x := tuple(islice(iterable, n)):
yield x

关于python - 如何获取字符串中所有字符的 zip。 zip 遗漏了最后一个字符,并且 itertools.zip_longest 不添加任何字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37725798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com