对嵌套列表中的列表进行排序

Ron*_*Ron 5 wolfram-mathematica

我有一个嵌套列表: {{9, 8, 7}, {8, 7, 6}, {7, 6, 5}, {6, 5, 4}, {5, 4, 3}, {4, 3, 2}, {3, 2, 1}}

我需要对列表中的列表进行排序以创建:

{{7, 8, 9}, {6, 7, 8}, {5, 6, 7}, {4, 5, 6}, {3, 4, 5}, {2, 3, 4}, (1, 2, 3}}

我该怎么做呢?

Mic*_*lat 9

您需要该Map函数,该函数将函数应用于列表的每个元素.

也就是说,Map[f, {1, 2, 3}]给出{f[1], f[2], f[3]}.

在这种情况下,您可以使用Map[Sort, list].Map还有一个中缀运算符,/@:

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