我有两个文件 .. 我使用循环法从第一个文件中读取一行,从第二个文件中读取第二行。
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/