我正在使用此功能

def convert_tuple(self, listobj, fields=['start', 'end', 'user']):
    return [(getattr(obj, field) for  field in fields)
            for obj in listobj]

我想要的输出应该是
[('2am', '5am', 'john'), ('3am', '5am', 'john1'), ('3am', '5am', 'john2') ]

上面函数的输出是
[genexp, genexp, genexp]

它的生成器表达式,我无法按需要扩展它

最佳答案

将gen-exp转换为tuple

def convert_tuple(self, listobj, fields=['start', 'end', 'user']):
    return [tuple(getattr(obj, field) for  field in fields)
            for obj in listobj]

10-08 08:21