gpt4 book ai didi

python - 属性错误 : generator object has no attribute 'sort'

转载 作者:行者123 更新时间:2023-11-28 21:35:02 26 4
gpt4 key购买 nike

我有两个文件 .. 我使用循环法从第一个文件中读取一行,从第二个文件中读取第二行。

def roundrobin(*iterables):
pending = len(iterables)
nexts = cycle(iter(it).next for it in iterables)
while pending:
try:
for next in nexts:
yield next()
except StopIteration:
pending -= 1
nexts = cycle(islice(nexts, pending))

然后:

c= roundrobin(a, b)

a 和 b 是列表。它如何通过排序进行循环?..我尝试使用

c.sort()

但是错误是

AttributeError: 'generator' object has no attribute 'sort'

我需要根据第一列(d/M/Y)的元素对c进行排序

最佳答案

如错误所示,生成器没有 sort 方法。您可以改为通过内置 sorted 耗尽发电机,它接受一个 iterable 作为输入。这是一个简单的例子:

def randoms(n):
import random
for _ in range(n):
yield random.randint(0, 10)

res = sorted(randoms(10)) # [1, 2, 4, 5, 6, 6, 6, 7, 8, 10]
res = randoms(10).sort() # AttributeError: 'generator' object has no attribute 'sort'

关于python - 属性错误 : generator object has no attribute 'sort' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52775382/

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