Skip to main content

filtering - Discard Elements of list with given criteria


I have the following list:


{{3506, 0.120908, 
0.213813, <|1 -> {7, 9, 18, 20}, 6 -> {15}|>}, {3726, 0.119108,
0.209133, <|1 -> {6, 8, 9, 13, 18, 20}, 3 -> {15}|>}, {3761,
0.116293,
0.203036, <|1 -> {6, 8, 14, 18, 20}, 2 -> {12}, 4 -> {15}|>}, {3946,
0.107619,
0.192869, <|3 -> {6}, 1 -> {7, 18, 20}, 2 -> {12}|>}, {4016,

0.104384,
0.190066, <|1 -> {6, 7, 10, 15, 18, 20}, 6 -> {12}|>}, {4086,
0.106909,
0.187285, <|5 -> {11, 12}, 4 -> {15}, 1 -> {18, 20}|>}, {4236,
0.103937,
0.188204, <|1 -> {5, 6, 8, 14, 18, 20}, 5 -> {12}|>}, {4271,
0.103541,
0.186671, <|1 -> {4, 7, 11, 13, 18, 20}, 5 -> {12},
2 -> {15}|>}, {4421, 0.101325,
0.186022, <|2 -> {5, 7}, 1 -> {6, 13, 18, 20}|>}, {4491, 0.101609,

0.180121, <|1 -> {4, 6, 7, 15, 18, 20}, 6 -> {11}|>}, {4491,
0.100645, 0.184125, <|3 -> {7}, 7 -> {10}, 1 -> {18, 20}|>}, {4526,
0.0989646,
0.17942, <|1 -> {5, 6, 14, 18, 20}, 2 -> {10}, 7 -> {12}|>}, {4561,
0.0945528,
0.175016, <|1 -> {5, 18, 20}, 5 -> {11}, 8 -> {12}|>}, {4816,
0.0968629,
0.172952, <|1 -> {3, 10, 18, 20}, 5 -> {11}, 6 -> {12},
2 -> {14}|>}, {4816, 0.100074,
0.171889, <|1 -> {6, 18, 20}, 2 -> {10, 14}, 10 -> {11}|>}, {4851,

0.0956618, 0.167787, <|15 -> {11}, 1 -> {12, 14, 18, 20}|>}, {4966,
0.0933867,
0.173685, <|2 -> {3}, 1 -> {5, 7, 10, 18, 20}, 5 -> {12}|>}, {5001,
0.0929784,
0.171669, <|1 -> {3, 4, 5, 11, 14, 18, 20}, 7 -> {12}|>}, {5186,
0.0923044,
0.169445, <|2 -> {4, 5, 11, 12}, 1 -> {6, 18, 20}|>}, {5221,
0.0938267,
0.16939, <|3 -> {3}, 1 -> {6, 18, 20}, 2 -> {10},
5 -> {12}|>}, {5256, 0.0900446,

0.167957, <|3 -> {5}, 5 -> {10, 12}, 1 -> {18, 20}|>}, {5256,
0.0953461,
0.167948, <|2 -> {4}, 1 -> {5, 13, 14, 15, 18, 20},
7 -> {11}|>}, {5291, 0.0919092,
0.164553, <|2 -> {5, 10}, 10 -> {11}, 1 -> {13, 18, 20}|>}, {5441,
0.0917546,
0.167778, <|1 -> {2, 7, 18, 20}, 3 -> {3, 12}, 2 -> {11}|>}, {5511,
0.091757,
0.165217, <|1 -> {4, 5, 7, 11, 18, 20}, 10 -> {10}|>}, {5511,
0.0931502,

0.163884, <|3 -> {4, 13}, 8 -> {11}, 1 -> {18, 20}|>}, {5581,
0.0952444,
0.163607, <|1 -> {2, 12, 18, 20}, 6 -> {10}, 7 -> {11},
3 -> {13}|>}, {5661, 0.0895339,
0.165808, <|1 -> {3, 10, 13, 15, 18, 20}, 5 -> {5}|>}, {5696,
0.0878405,
0.162595, <|4 -> {3}, 1 -> {4, 11, 18, 20}, 5 -> {12}|>}, {5731,
0.0884125,
0.162495, <|1 -> {2, 3, 18, 20}, 2 -> {5, 11}, 4 -> {10},
3 -> {12}|>}, {5766, 0.0904722,

0.161405, <|1 -> {2, 13, 18, 20}, 2 -> {4, 12}, 5 -> {10},
4 -> {11}|>}, {5766, 0.0921969,
0.161218, <|1 -> {3, 4, 6, 11, 18, 20}, 11 -> {10}|>}, {5881,
0.0843905,
0.161821, <|2 -> {3}, 5 -> {5}, 1 -> {12, 18, 20}|>}, {5916,
0.0864502,
0.160761, <|1 -> {3, 10, 13, 18, 20}, 2 -> {4, 11},
3 -> {5}|>}, {5951, 0.0879725,
0.160539, <|4 -> {3}, 1 -> {5, 13, 18, 20}, 3 -> {10, 12}|>}, {5986,
0.0892943,

0.159927, <|2 -> {3, 5}, 9 -> {10}, 1 -> {15, 18, 20}|>}, {6021,
0.0897165,
0.158098, <|1 -> {2, 14, 18, 20}, 2 -> {4}, 8 -> {10},
4 -> {11}|>}, {6126, 0.0921288,
0.156535, <|20 -> {10}, 2 -> {13}, 1 -> {18, 20}|>}, {6136,
0.0837377, 0.157748, <|6 -> {4}, 1 -> {5, 10, 11, 18, 20}|>}, {6206,
0.0858319,
0.157381, <|1 -> {2, 5, 12, 18, 20}, 3 -> {4}, 7 -> {10}|>}, {6206,
0.0867804,
0.157329, <|2 -> {2, 3}, 1 -> {5, 12, 18, 20}, 3 -> {10},

4 -> {11}|>}, {6206, 0.0872168,
0.157297, <|4 -> {3}, 1 -> {5, 12, 14, 18, 20}, 6 -> {10}|>}, {6241,
0.0862107,
0.155018, <|2 -> {3, 4, 11}, 9 -> {10}, 1 -> {18, 20}|>}, {6646,
0.085206,
0.154682, <|2 -> {2}, 3 -> {3, 10},
1 -> {4, 5, 14, 18, 20}|>}, {6751, 0.0862941,
0.152039, <|3 -> {2}, 1 -> {3, 11, 18, 20}, 12 -> {10}|>}}

I would like to discard the rows who's fourth element's sum of the key numbers times the length of the respective value vector is less than 4 and more than 12.



Sorry for the format of the list, but I can't find a way to code-format it.



Answer



Just an alternative:


Select[
list,
4 <= Tr @ KeyValueMap[# Length[#2] &, #[[-1]] ] <= 12 &
]

Comments

Popular posts from this blog

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

How to thread a list

I have data in format data = {{a1, a2}, {b1, b2}, {c1, c2}, {d1, d2}} Tableform: I want to thread it to : tdata = {{{a1, b1}, {a2, b2}}, {{a1, c1}, {a2, c2}}, {{a1, d1}, {a2, d2}}} Tableform: And I would like to do better then pseudofunction[n_] := Transpose[{data2[[1]], data2[[n]]}]; SetAttributes[pseudofunction, Listable]; Range[2, 4] // pseudofunction Here is my benchmark data, where data3 is normal sample of real data. data3 = Drop[ExcelWorkBook[[Column1 ;; Column4]], None, 1]; data2 = {a #, b #, c #, d #} & /@ Range[1, 10^5]; data = RandomReal[{0, 1}, {10^6, 4}]; Here is my benchmark code kptnw[list_] := Transpose[{Table[First@#, {Length@# - 1}], Rest@#}, {3, 1, 2}] &@list kptnw2[list_] := Transpose[{ConstantArray[First@#, Length@# - 1], Rest@#}, {3, 1, 2}] &@list OleksandrR[list_] := Flatten[Outer[List, List@First[list], Rest[list], 1], {{2}, {1, 4}}] paradox2[list_] := Partition[Riffle[list[[1]], #], 2] & /@ Drop[list, 1] RM[list_] := FoldList[Transpose[{First@li...

front end - keyboard shortcut to invoke Insert new matrix

I frequently need to type in some matrices, and the menu command Insert > Table/Matrix > New... allows matrices with lines drawn between columns and rows, which is very helpful. I would like to make a keyboard shortcut for it, but cannot find the relevant frontend token command (4209405) for it. Since the FullForm[] and InputForm[] of matrices with lines drawn between rows and columns is the same as those without lines, it's hard to do this via 3rd party system-wide text expanders (e.g. autohotkey or atext on mac). How does one assign a keyboard shortcut for the menu item Insert > Table/Matrix > New... , preferably using only mathematica? Thanks! Answer In the MenuSetup.tr (for linux located in the $InstallationDirectory/SystemFiles/FrontEnd/TextResources/X/ directory), I changed the line MenuItem["&New...", "CreateGridBoxDialog"] to read MenuItem["&New...", "CreateGridBoxDialog", MenuKey["m", Modifiers-...