使用Mathematica正确收集/收集

bar*_*ter 5 wolfram-mathematica

如何使用Mathematica的Gather/Collect/Transpose函数进行转换:

{ { {1, foo1}, {2, foo2}, {3, foo3} }, { {1, bar1}, {2, bar2}, {3, bar3} } } 
Run Code Online (Sandbox Code Playgroud)

{ {1, foo1, bar1}, {2, foo2, bar2}, {3, foo3, bar3} } 
Run Code Online (Sandbox Code Playgroud)

编辑:谢谢!我希望有一个简单的方法,但我猜不是!

Dr.*_*ius 8

也许更容易:

tst = {{{1, foo1}, {2, foo2}, {3, foo3}}, {{1, bar1}, {2, bar2}, {3,  bar3}}};

GatherBy[Flatten[tst, 1], First] /. {{k_, n_}, {k_, m_}} -> {k, n, m}
(*
-> {{1, foo1, bar1}, {2, foo2, bar2}, {3, foo3, bar3}}
*)
Run Code Online (Sandbox Code Playgroud)


Leo*_*rin 7

这是你的清单:

tst = {{{1, foo1}, {2, foo2}, {3, foo3}}, {{1, bar1}, {2, bar2}, {3,  bar3}}}
Run Code Online (Sandbox Code Playgroud)

这是一种方式:

In[84]:= 
Flatten/@Transpose[{#[[All,1,1]],#[[All,All,2]]}]&@
  GatherBy[Flatten[tst,1],First]

Out[84]= {{1,foo1,bar1},{2,foo2,bar2},{3,foo3,bar3}}
Run Code Online (Sandbox Code Playgroud)

编辑

这是一个完全不同的版本,只是为了好玩:

In[106]:= 
With[{flat = Flatten[tst,1]},
   With[{rules = Dispatch[Rule@@@flat]},
       Map[{#}~Join~ReplaceList[#,rules]&,DeleteDuplicates[flat[[All,1]]]]]]

Out[106]= {{1,foo1,bar1},{2,foo2,bar2},{3,foo3,bar3}}
Run Code Online (Sandbox Code Playgroud)

编辑2

这是另一种方法,使用链接列表和内部函数来累积结果:

In[113]:= 
Module[{f},f[x_]:={x};
  Apply[(f[#1] = {f[#1],#2})&,tst,{2}];
  Flatten/@Most[DownValues[f]][[All,2]]]

Out[113]= {{1,foo1,bar1},{2,foo2,bar2},{3,foo3,bar3}}
Run Code Online (Sandbox Code Playgroud)

编辑3

好吧,对于那些认为上述所有内容过于复杂的人来说,这是一个非常简单的基于规则的解决方案:

In[149]:= 
GatherBy[Flatten[tst, 1], First] /. els : {{n_, _} ..} :> {n}~Join~els[[All, 2]]

Out[149]= {{1, foo1, bar1}, {2, foo2, bar2}, {3, foo3, bar3}}
Run Code Online (Sandbox Code Playgroud)

  • @David:标准的`x_`实际上是`x:_`的缩写,但前者很常见,很多人都不认识后者.两者都被读作"名为`x`的模式匹配`Blank []`". (4认同)

WRe*_*ach 6

MapThread

如果保证"foo"和"bar"子列表彼此对齐(如示例中所示),并且如果您将考虑使用Gather/ Collect/ 以外的函数Transpose,那么MapThread就足够了:

data={{{1,foo1},{2,foo2},{3,foo3}},{{1,bar1},{2,bar2},{3,bar3}}};

MapThread[{#1[[1]], #1[[2]], #2[[2]]}&, data]
Run Code Online (Sandbox Code Playgroud)

结果:

{{1, foo1, bar1}, {2, foo2, bar2}, {3, foo3, bar3}}
Run Code Online (Sandbox Code Playgroud)

模式匹配

如果列表对齐,您还可以使用直接模式匹配和替换(尽管我不建议将此方法用于大型列表):

data //.
  {{h1___, {x_, foo__}, t1___}, {h2___, {x_, bar_}, t2___}} :>
  {{h1, {x, foo, bar}, t1}, {h2, t2}} // First
Run Code Online (Sandbox Code Playgroud)

母猪/粒

对于未对齐列表的更有效方法使用SowReap:

Reap[Cases[data, {x_, y_} :> Sow[y, x], {2}], _, Prepend[#2, #1] &][[2]]
Run Code Online (Sandbox Code Playgroud)


fay*_*sou 5

以下是我如何使用我在Mathematica工具包中的内容中发布的SelectEquivalents版本

l = {{{1, foo1}, {2, foo2}, {3, foo3}}, {{1, bar1}, {2, bar2}, {3, bar3}}};

SelectEquivalents[
   l
   ,
   MapLevel->2
   ,
   TagElement->(#[[1]]&)
   ,
   TransformElement->(#[[2]]&)
   ,
   TransformResults->(Join[{#1},#2]&)
]
Run Code Online (Sandbox Code Playgroud)

这种方法非常通用.我曾经使用像GatherBy这样的函数来处理我在Monte-Carlo模拟中生成的巨大列表.现在使用SelectEquivalents进行此类操作的实现更加直观.此外,它基于Reap和Sow的组合,这在Mathematica中非常快.