4
$\begingroup$

I have a long list of lists of values and replacement rules as:

list = {
 {{0.5, {r0 -> 0.1, a -> 2.3}}, {2.0, {r0 -> 0.8, a -> 0.4}}, {1.2, {r0 -> 1.1, a -> 0.7}}}
 {{4.2, {r0 -> 5.1, a -> 0.9}}, {0.1, {r0 -> 1.3, a -> 7.4}}, {1.0, {r0 -> 3.3, a -> 0.4}}}
 {{1.0, {r0 -> 1.1, a -> 1.9}}, {0.7, {r0 -> 1.2, a -> 2.2}}, {0.1, {r0 -> 4.8, a -> 3.2}}}
   (*AND SO ON till more than 1000 lists*)
}

I would like to make another list, with the second elements of the previous list, such that the first one is the minimum of the each row... I mean, something like

{ {r0 -> 0.1, a -> 2.3}
, {r0 -> 1.3, a -> 7.4}
, {r0 -> 4.8, a -> 3.2}    
, (*...*)
}
$\endgroup$
  • $\begingroup$ the minimum has to be the first element, not in the replacement list; in the last row is {0.1, {r0 -> 4.8, a -> 3.2}} $\endgroup$ – user115376 Apr 25 '17 at 12:16
  • $\begingroup$ it worked great, thanks @Kuba $\endgroup$ – user115376 Apr 25 '17 at 13:27
4
$\begingroup$
Map[First@Cases[#, {x_ /; x == Min[First @@@ #[[All, 1]]], pat : __ } :> 
 pat , Infinity] &, list]

or in a more succint and better way proposed by Kuba:

Last@*First@*MinimalBy[First]/@ list

another method that works (suggested by pfactors):

Last@First@Sort@# &/@ list
$\endgroup$
  • 1
    $\begingroup$ Probably worth noting Ali that your version (although probably slower) would work in MMA < 10. As a side note I think Last@First@Sort@# & /@ list might work as well. $\endgroup$ – pfactors Apr 25 '17 at 17:22
3
$\begingroup$

In:

Clear[a, r0, list]
list = {{{0.5, {r0 -> 0.1, a -> 2.3}}, {2.0, {r0 -> 0.8, 
     a -> 0.4}}, {1.2, {r0 -> 1.1, a -> 0.7}}}, {{4.2, {r0 -> 5.1, 
     a -> 0.9}}, {0.1, {r0 -> 1.3, a -> 7.4}}, {1.0, {r0 -> 3.3, 
     a -> 0.4}}}, {{1.0, {r0 -> 1.1, a -> 1.9}}, {0.7, {r0 -> 1.2, 
     a -> 2.2}}, {0.1, {r0 -> 4.8, a -> 3.2}}}};

list // Map@MinimalBy@First // Last @@@ # & 

Out:

{{r0 -> 0.1, a -> 2.3}, {r0 -> 1.3, a -> 7.4}, {r0 -> 4.8, a -> 3.2}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.