Tag Info

Hot answers tagged

8

Thread@list gives {{1, 2, 3, a, 4}, {1, 2, 3, b, 4}, {1, 2, 3, c, 4}}


8

Fold[DeleteCases[##, 1, 1] &, a, b] {"A", "A", "B", "C"}


7

For efficiency, treat them as variables and let Plus do the work: List @@ (Plus @@ a - Plus @@ b) and convert to a list if you want: {"A", 2 "B", -3 "C"} /. (n_*v_ :> Sequence @@ ConstantArray[v,n]) (* => {"A", "B"} *) Update Kuba's solution is elegant but not efficient nor does it maintain a sorted order, look at the performance: In[1]:= ...


5

Join @@ (ConstantArray[#, 3000000] & /@ {1, 2, 3}); performs a little better. Needs["GeneralUtilities`"] BenchmarkPlot[{Function[x, Join @@ (ConstantArray[#, x] & /@ {1, 2, 3})], Join @@ Transpose@Table[{3, 2, 1}, {#}] &}, # &, PowerRange[100, 100000000], "IncludeFits" -> True]


5

You can use: Distribute[l1, List]


5

Maybe MapThread gives the levelspec control you need: lst = {{{a, b}, {c, d}}, {{w, x}, {y, z}}}; g @@ MapThread[f, lst, 1] (* or just g @@ MapThread[f, lst] -- thanks: Mr.W *) (* g[f[{a,b},{w,x}],f[{c,d},{y,z}]] *) g @@ MapThread[f, lst, 2] (* g[{f[a,w],f[b,x]},{f[c,y],f[d,z]}] *) or, g @@ Thread[f @@ lst] (* g[f[{a,b},{w,x}],f[{c,d},{y,z}]] *) or ...


4

This probably doesn't address the full scope of your question but for the particular example you could use MapThread and Apply: ex1 = {{a, b}, {c, d}}; ex2 = {{w, x}, {y, z}}; g @@ MapThread[f, {ex1, ex2}] g[f[{a, b}, {w, x}], f[{c, d}, {y, z}]] Or Transpose and Apply: g @@ f @@@ ({ex1, ex2}\[Transpose]) g[f[{a, b}, {w, x}], f[{c, d}, {y, z}]]


4

This matches your example, but I had to get only the first three elements of each line (you didn't mention it). Select[cdatalist, #[[3]] =!= Failed &][[All, ;; 3]] Or, as per @belisarius suggestion (roughly twice as fast!) Cases[cdatalist, Except[{_, _, Failed, ___}]][[All, 1 ;; 3]] Or, "inspired" by @Gerli: Cases[cdatalist , {a_, b_, c : ...


4

Here is an approach: Tuples[Replace[l1, x_?AtomQ :> {x}, {1}]] yields: (*{{1, 2, a, 3, d}, {1, 2, a, 3, e}, {1, 2, b, 3, d}, {1, 2, b, 3, e}, {1, 2, c, 3, d}, {1, 2, c, 3, e}}*)


4

This is a straightforward application of GatherBy and Map (/@): First /@ GatherBy[mydata, First] (* {{1, a, aa}, {2, d, dd}, {3, f, ff}, {4, i, ii}, {5, n, nn}, {7, p, pp}} *) You could also use Part ([[]]) to get the first element of each group. GatherBy[mydata, First][[All,1]]


4


3

To reconstruct the result somehow: reconstructF=Join@@ConstantArray@@@Normal@#&; reconstructF@Merge[Total][{Counts[a],- Counts[b]}] (* {"A", "A", "B", "C"} *)


3

Could it be that you're looking for the function Fold rather than Nest? Fold[f[n - #2, #1] &, a, Range[5]] f[-5 + n, f[-4 + n, f[-3 + n, f[-2 + n, f[-1 + n, a]]]]]


3

ClearAll[a, b, c, x, y, z, list, func]; list = {{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}}; func[x_, y_, z_, a_, b_, c_] := {a, b, c}.{x, y, z} func[##, a, b, c] & @@ list[[1]] (* a x1+b y1+c z1 *) func[## & @@ #, a, b, c] &@list[[1]] (* a x1+b y1+c z1 *) func[##, a, b, c] & @@@ list (* {a x1+b y1+c z1,a x2+b y2+c z2,a x3+b y3+c z3} *) ...


3

As you expect monotonic "smooth" behavior, a simple solution is to z-score the differences. diff = data[[All, 2]] // Differences; mn = Mean[diff] std = StandardDeviation[diff] (* 3 std is bad *) bad = Position[diff, x_ /; Abs[x] > Abs[mn + 3 std]] ListPlot[data, PlotRange -> All] ListPlot[data[[bad // Flatten]], PlotStyle -> Red] Show[%, %%] ...


3

data = {CountryData[#, "PopulationGrowth"], CountryData[#, "LifeExpectancy"]} & /@ CountryData[]; data2 = Select[data, FreeQ[#, _Missing] &]; data3 = DeleteCases[data, _?(! FreeQ[#, _Missing] &)]; data4 = Pick[data, FreeQ[#, _Missing] & /@ data]; data5 = (data /. {_Missing, _} | {_, _Missing} :> Sequence[]); data6 = Cases[data, ...


2

I am adding this one for its novelty, but I would not use it in production as it is to uncontrolled: Block[{Missing}, Missing /: {_Missing, _} := Sequence[]; Missing /: {_, _Missing} := Sequence[]; data ] // Length (* 228 *) But, it operates by rewriting the behavior of Missing when it is found in a List.


2

plotOne[g_Graph] := Module[{probs, purgedTab, r = Range@Max@VertexDegree@g}, probs = {#, Probability[x == #, x \[Distributed] VertexDegree[g]]} & /@ r; purgedTab = DeleteCases[probs, {x_, y_} /; x y == 0]; ListLinePlot[Log@purgedTab, Filling -> Axis, Mesh -> Full, MeshStyle -> Directive[PointSize[Large], Black], ...


2

n = 20 (*even*) f = Interpolation[Transpose[{data[[All, 1]], Join[ data[[;; (n/2 - 1), 2]] , MovingAverage[data[[All, 2]], n] , data[[-n/2 ;;, 2]] ]}]] GraphicsColumn[{ListPlot[data], ListPlot[ Select[ data , Abs[f[#[[1]]] - #[[2]]] < .2 & ] ]}]


2

A common approach to removing outliers is to use an order statistic filter. The simplest of these is the MedianFilter: x = data[[All, 1]]; ySmoothed = MedianFilter[data[[All, 2]], 5]; ListPlot[Transpose[{x, ySmoothed}]]


2

Try Cases. If you want the third column to be Real: Cases[cdatalist, {_, _, _Real, __}][[All, 1 ;; 3]] or Cases[cdatalist[[All, 1 ;; 3]], {_, _, _Real}] depending on whether you want to shorten the list before or after filtering. It's more general if you filter out everything that is not Failed or $Failed: Cases[cdatalist[[All, 1 ;; 3]], {_, _, ...


1

ReplacePart[list, 4 -> #] & /@ list[[4]] (*{{1, 2, 3, a, 4}, {1, 2, 3, b, 4}, {1, 2, 3, c, 4}}*)


1

With[{pos = Position[list, _List][[1, 1]]}, ReplacePart[list, pos -> #] & /@ list[[pos]]] (*{{1, 2, 3, a, 4}, {1, 2, 3, b, 4}, {1, 2, 3, c, 4}}*)


1

If (a) preserving the order of the minuend list and (b) respecting the order of the subtrahend list are important to you (or to someone else reading this post), here's an interesting solution using SequenceAlignment: a = {"C", "A", "A", "A", "B", "B", "C", "A"}; b = {"B", "A", "D"}; listComplement[a_List, b_List] := Join @@ Cases[SequenceAlignment[a, ...


1

a = {2 "A", "A", "A", "B", "B", "C"}; b = {"A", "B"}; Delete[a, First[Position[a, #, 1]] & /@ b] (*{2 "A", "A", "B", "C"}*)


1

Join @@ Transpose@Table[{3, 2, 1}, {5}] Flatten@Transpose@Table[{3, 2, 1}, {5}] Join @@ (ConstantArray[#, 5] & /@ {3, 2, 1}) Join @@ (Table[#, {5}] & /@ {3, 2, 1}) all give (* {3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1} *)


1

Aside from all the other good answers which attack your problem from the beginning, a handy thing to remember is a way to tidy it up at the end: datalist //. {} :> Sequence[] This makes all your empty (sub)lists go away.


1

Pick[#, (#[[-1]] =!= Failed) & /@ #] &@cdatalist[[All, ;; 3]] DeleteCases[cdatalist[[All, ;; 3]], {_, _, Failed}] DeleteCases[cdatalist, {_, _, Failed, ___}][[All, ;; 3]] all give (* {{1., 1.31175, 1.}, {1., 19.6628, 0.990079}, {1., 40.6208, 0.980588}, {15., 1.31344, 1.}} *)


1

I'm hardly sure that I have understood your question fully, but does this give you the result that you want? Table[Probability[x == k, x \[Distributed] VertexDegree[j]], {j, allgraphs}, {k, Max[VertexDegree[j]]}] You need to specify the j iterator first, as the specification of k depends on j. On a more general note, I would say that if I want to ...


1

Last @ Nest[{ #[[1]] - 1, f1[ ff[ #[[1]], #[[2]] ] ] } &, {n, list}, 3] f1[ff[-2 + n, f1[ff[-1 + n, f1[ff[n, list]]]]]]



Only top voted, non community-wiki answers of a minimum length are eligible