繁体   English   中英

在python中通过列表理解生成的排序元组

[英]Sorting tuple genereated by list comprehension in python

我在排序由列表理解创建的单个元组时遇到麻烦。 说我们有:

words = [(a, b, c) for a in al for b in bl for c in cl]

现在,我想通过执行以下操作对每个元组(a,b,c)进行排序:

map(lambda x: sorted(x), words)

这给了我错误:'tuple'对象不可调用。

我也尝试过:

for i in range(len(words)):
    out = [words[i][0], words[i][1], words[i][2]]
    print out.sort()

打印出一堆无。

我想念什么? 提前致谢。

您可以将元组排序为创建的一部分:

words = [sorted((a, b, c)) for a in al for b in bl for c in cl]

请注意,这将为您提供一个列表列表,而不是元组列表,因为sorted返回一个列表。 如果您真的想要元组,则必须做

words = [tuple(sorted((a, b, c))) for a in al for b in bl for c in cl]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM