Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中列表理解生成的排序元组_Python_Sorting_Tuples_List Comprehension - Fatal编程技术网

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

python中列表理解生成的排序元组,python,sorting,tuples,list-comprehension,Python,Sorting,Tuples,List Comprehension,我无法对列表创建的单个元组进行排序。 假设我们有: 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][

我无法对列表创建的单个元组进行排序。 假设我们有:

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]

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

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]

您可能创建了名为
map
sorted
的变量,覆盖了同名函数。您可能创建了名为
map
sorted
的变量,覆盖了同名函数。