AttributeError:生成器对象没有属性'sort'

use*_*236 2 python sorting iterator generator

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

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))
Run Code Online (Sandbox Code Playgroud)

然后:

c= roundrobin(a, b)
Run Code Online (Sandbox Code Playgroud)

a和b是列表.如何循环排序?我尝试使用

c.sort()
Run Code Online (Sandbox Code Playgroud)

但错误是

AttributeError:'generator'对象没有属性'sort'

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

jpp*_*jpp 5

如错误所示,生成器没有sort方法.您可以通过内置函数耗尽生成器,内置函数sorted接受迭代作为输入.这是一个简单的例子:

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'
Run Code Online (Sandbox Code Playgroud)