New answers tagged

10

For this use case, I would encourage you to use Map and Replace rather than Cases, since you can use multiple rules for the extraction: Map[Replace[{{___, {}} :> Null, {___, {x_}} :> x}], dat1] (* {Null, 1, 2} *) As an alternative, you can use Replace with a level spec: Replace[dat1, {{___, {}} :> Null, {___, {x_}} :> x}, 1] (* {Null, 1, 2} *)


9

Here I treat the contents of the empty element as Null and I assume either 1 element in the nested list or no element: Cases[dat1, {x_ : Null} :> x, 2] (* returns: {Null, 1, 2} *)


2

Using TakeLargestBy roots = {{B -> 0.622096}, {B -> 1.03445}, {B -> 48.6767}}; TakeLargestBy[roots, #[[-1, -1]] &, 1] (* {{B -> 48.6767}} *)


4

I like the neat functionality of Ordering: Extract[#,Ordering[#,-1]]&@roots {B->48.6767} If you prefer Cases: Cases[#,List[Rule[b_,Max@Values@#]]]&@roots {{B->48.6767}}


3

roots = {{B -> 0.622096}, {B -> 1.03445}, {B -> 48.6767}}; ClearAll[f1, f2, f3] f1 = Merge[Max]; f2 = Last @* Sort; f3 = #[[Ordering[#, -1]]] &; f1 @ roots <|B -> 48.6767|> If you need a list: Normal @ % {B -> 48.6767} f2 @ roots {B -> 48.6767} f3 @ roots {{B -> 48.6767}}


7

roots /. soln // Max Works and uses core functions.


4

Try this: expr = {{B -> 0.622096}, {B -> 1.03445}, {B -> 48.6767}}; B -> Max[Transpose[expr][[1]] /. Rule[B, x_] -> x] (* B -> 48.6767 *) Have fun!


10

Perfect case for MaximalBy and the operator form of Lookup. roots = {{B -> 0.622096}, {B -> 1.03445}, {B -> 48.6767}}; MaximalBy[roots, Lookup[B]] (* {{B -> 48.6767}} *)


4

Clear["Global`*"] pool = Table[Range[3], 7] // Flatten; Rather than produce all tuples, produce a tuple on demand choice := Module[{ch = RandomSample[pool]}, While[Count[Most[ch] - Rest[ch], 0] != 15, ch = RandomSample[pool]]; ch] choice (* {2, 2, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 2, 2} *) For multiple tuples Table[choice,...


1

For a length of 18 (6 groups of 1, 2, and 3) one finds 17,153,136 permutations. n = 6; data = Flatten[Table[{1, 2, 3}, {n}]]; p = Permutations[data]; Length[p] (* 17153136 *) In general for a length of 3 n there will be (3 n)!/(n!^3) permutations. For 3 n = 21 there will be 399,072,960 permuations.


2

criteria = {criterion1, criterion2} = {GreaterThan[.17], LessThan[.19]}; Join @@ MapThread[Select[#]@#2 &, {criteria, {list1, list2}}] {0.172804, 0.175326, 0.17098, 0.172789, 0.173099, 0.190308, 0.173863,0.143799, 0.181231} "how can I also find the positions in lists1 and 2 of each element that is appented in the joint list?" KeySelect[...


4

We can use a modification of the answer here to do this. lst = {{{x1, -1}, {{a1, 1}, {c1, d1}}}, {{x2, 1 }, {{a2, -1}, {c2, d2}}}, {{x3, -1}, {{a3, 1}, {c3, d3}}}, {{x4, 1}, {{a4, -1}, {c4, d4}}}}; Extract[List@*First/@Position[a_/;a>=0][First/@lst]][lst] {{{x2,1},{{a2,-1},{c2,d2}}},{{x4,1},{{a4,-1},{c4,d4}}}} Or Cases[{{x_,y_},{a__List}}:&...


3

list/.{_,_?Negative}:>Nothing


Top 50 recent answers are included