对嵌套列表中的列表进行排序 [英] Sorting lists inside nested lists
本文介绍了对嵌套列表中的列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个嵌套列表:{{9, 8, 7}, {8, 7, 6}, {7, 6, 5}, {6, 5, 4}, {5, 4, 3}, {4, 3, 2}, {3, 2, 1}}
我需要对列表中的列表进行排序以创建:
I need to sort the lists within the list to create:
{{7, 8, 9}, {6, 7, 8}, {5, 6, 7}, {4, 5, 6}, {3, 4, 5}, {2,3, 4}, (1, 2, 3}}
我该怎么做?
推荐答案
您想要 Map
函数,将函数应用于列表的每个元素.
You want the Map
function, which applies a function to each element of a list.
即Map[f, {1, 2, 3}]
给出{f[1], f[2], f[3]}
.
在这种情况下,您可以使用 Map[Sort, list]
.Map
还有一个中缀操作符,/@
:
In this case, you can use Map[Sort, list]
. Map
also has an infix operator, /@
:
In[1]:= Map[Sort, {{9, 8, 7}, {8, 7, 6}, {7, 6, 5}, {6, 5, 4},
{5, 4, 3}, {4, 3, 2}, {3, 2, 1}}]
Out[1]= {{7, 8, 9}, {6, 7, 8}, {5, 6, 7}, {4, 5, 6},
{3, 4, 5}, {2, 3, 4}, {1, 2, 3}}
In[2]:= Sort /@ {{9, 8, 7}, {8, 7, 6}, {7, 6, 5}, {6, 5, 4},
{5, 4, 3}, {4, 3, 2}, {3, 2, 1}}
Out[2]= {{7, 8, 9}, {6, 7, 8}, {5, 6, 7}, {4, 5, 6},
{3, 4, 5}, {2, 3, 4}, {1, 2, 3}}
这篇关于对嵌套列表中的列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文