gpt4 book ai didi

python - 在不调用itertools的grouper函数的情况下分割python列表但出现错误

转载 作者:行者123 更新时间:2023-11-30 23:37:18 26 4
gpt4 key购买 nike

在此代码中,石斑鱼函数工作正常,但是如果我在不调用该函数的情况下执行此操作。它抛出一个错误

类型错误:izip_longest 参数 #1 必须支持迭代

from itertools import *

def grouper(n, iterable, fillvalue=None):
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)


x = [1,2,3]

args = [iter(x)] * 2
l = izip_longest(None , *args )
#l = grouper(2,x)
print [x for x in l]

最佳答案

所有位置参数都应该是可迭代的,而不是填充值。 Pass fillvalue as a keyword argument :

it = izip_longest(*iterables, fillvalue=None)

如果fillvalueNone;你可以省略它:

it = izip_longest(*iterables)

关于python - 在不调用itertools的grouper函数的情况下分割python列表但出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15711282/

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