例如:
{{a, b}, {c, {d}, e}, {f, {g, h}}}
希望实现的结果为:
{{a, b}, {c, d, e}, {f, g, h}}
再例如:
{{{a},{b},{c},d},{e}}
希望实现的结果为:
{{a,b,c,d},{e}}
我使劲倒弄Flatten,可是怎么弄都好像不对.....
===========================================================
利用TreeForm对最内层下一个定义。
只有TreeForm产生的图的全体最终节点(最内层)中的元素被取出,并且放入到对应的上一节点内(压平)。又如:
以及:
还有:
================================================
发现了一个比较严重的问题。好像各位都完全没考虑列表元素是符号值的情况啊。
出错示例:
In[94]:= flattenMostInner=Map[#/.{{x___}:>x,x___:>x}&,#,{Depth[#]-2}]&;
flattenMostInner2=Map[#/.{x_List:>Flatten@x}&,#,{Depth[#]-3}]&;
flattenMostInner3=Apply[Sequence,#,{Depth[#]-2}]&;
test0={{{a},{b}},{{c,{d}},{e},{f},{{g},{h}}}};
test1=List[List[List[Hue[0.5107930946784643`,0.8354307161342199`,0.7718521452428755`]],List[Hue[0.5113169196285673`,0.8358213579394708`,0.7713657815095133`]]],List[List[Hue[0.192618226271086`,0.8755716287620745`,0.7116923362420707`],List[Hue[0.5107930946784643`,0.8354307161342199`,0.7718521452428755`]]],List[Hue[0.3731282181502969`,0.789529229678425`,0.8195909513398986`]],List[Hue[0.4076579648111174`,0.7921461731652201`,0.8172922377294225`]],List[List[Hue[0.192618226271086`,0.8755716287620745`,0.7116923362420707`]],List[Hue[0.3731282181502969`,0.789529229678425`,0.8195909513398986`]]]]];
test2={{{f[a]},{f[b]}},{{g[a],{g[b]}},{f[a]},{g[e]},{{h[a]},{h[b]}}}};
test3=
flattenMostInner@test0
flattenMostInner2@test0
flattenMostInner3@test0
flattenMostInner@test1//FullForm
flattenMostInner2@test1//FullForm
flattenMostInner3@test1//FullForm
flattenMostInner@test2
flattenMostInner2@test2
flattenMostInner3@test2
Out[100]= {{{a},{b}},{{c,d},{e},{f},{g,h}}}
Out[101]= {{{a},{b}},{{c,d},{e},{f},{g,h}}}
Out[102]= {{{a},{b}},{{c,d},{e},{f},{g,h}}}
Out[103]//FullForm= List[List[List[Hue[0.5107930946784643`,0.8354307161342199`,0.7718521452428755`]],List[Hue[0.5113169196285673`,0.8358213579394708`,0.7713657815095133`]]],List[List[Hue[0.192618226271086`,0.8755716287620745`,0.7116923362420707`],List[Hue[0.5107930946784643`,0.8354307161342199`,0.7718521452428755`]]],List[Hue[0.3731282181502969`,0.789529229678425`,0.8195909513398986`]],List[Hue[0.4076579648111174`,0.7921461731652201`,0.8172922377294225`]],List[List[Hue[0.192618226271086`,0.8755716287620745`,0.7116923362420707`]],List[Hue[0.3731282181502969`,0.789529229678425`,0.8195909513398986`]]]]]
Out[104]//FullForm= List[List[List[Hue[0.5107930946784643`,0.8354307161342199`,0.7718521452428755`]],List[Hue[0.5113169196285673`,0.8358213579394708`,0.7713657815095133`]]],List[List[Hue[0.192618226271086`,0.8755716287620745`,0.7116923362420707`],List[Hue[0.5107930946784643`,0.8354307161342199`,0.7718521452428755`]]],List[Hue[0.3731282181502969`,0.789529229678425`,0.8195909513398986`]],List[Hue[0.4076579648111174`,0.7921461731652201`,0.8172922377294225`]],List[List[Hue[0.192618226271086`,0.8755716287620745`,0.7116923362420707`]],List[Hue[0.3731282181502969`,0.789529229678425`,0.8195909513398986`]]]]]
Out[105]//FullForm= List[List[List[Hue[0.5107930946784643`,0.8354307161342199`,0.7718521452428755`]],List[Hue[0.5113169196285673`,0.8358213579394708`,0.7713657815095133`]]],List[List[Hue[0.192618226271086`,0.8755716287620745`,0.7116923362420707`],List[0.5107930946784643`,0.8354307161342199`,0.7718521452428755`]],List[Hue[0.3731282181502969`,0.789529229678425`,0.8195909513398986`]],List[Hue[0.4076579648111174`,0.7921461731652201`,0.8172922377294225`]],List[List[0.192618226271086`,0.8755716287620745`,0.7116923362420707`],List[0.3731282181502969`,0.789529229678425`,0.8195909513398986`]]]]
Out[106]= {{{f[a]},{f[b]}},{{g[a],{g[b]}},{f[a]},{g[e]},{{h[a]},{h[b]}}}}
Out[107]= {{{f[a]},{f[b]}},{{g[a],{g[b]}},{f[a]},{g[e]},{{h[a]},{h[b]}}}}
Out[108]= {{{f[a]},{f[b]}},{{g[a],{b}},{f[a]},{g[e]},{{a},{b}}}}
==============================================================
此外,必须更加精密地定义最底层的概念。节点的长度,理应是根据FullFrom的头部为List的节点作计算的。
因此例如:
{a, {b, g[g[g[{c, {d}}]]]}}
应该得到:
{a, b, g[g[g[{c, {d}}]]]}
诸如以下的:
{a -> {1, {2, {3}}}, {b :> {2, {3}}, {c, {d}}}}
应该得到:
{a -> {1, {2, {3}}}, {b :> {2, {3}}, {c, d}}}
因为:
In[116]:= {a->{1,{2,{3}}},{b:>{2,{3}},{c,{d}}}}//FullForm
Out[116]//FullForm= List[Rule[a,List[1,List[2,List[3]]]],List[RuleDelayed[b,List[2,List[3]]],List[c,List[d]]]]
理应只有List[C,List[d]]被压平。
==========================================
为什么Atom的识别是无歧义非人为的:
只有List作为头部的节点才能计算层数。在FullForm和TreeForm是掩盖不了的。又如:
蓝色线是表示满足最底层的分叉,蓝圈是对于flattenMostInner有意义而不是视为一个单独Atom的节点。本质上来说这是一个图论问题。