1
$\begingroup$

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.

| improve this question | | | | |
$\endgroup$
  • $\begingroup$ Let's take one row for example, {3761, 0.116293, 0.203036, <|1 -> {6, 8, 14, 18, 20}, 2 -> {12}, 4 -> {15}|>} the third element is 0.203036. What numbers, in that row, are you trying to multiply in order to get a number between 4 and 12? $\endgroup$ – Jason B. May 4 '16 at 7:31
  • $\begingroup$ 1 times Length[{6, 8, 14, 18, 20}] + 2 times 1 +4 times 1 $\endgroup$ – Mirko Aveta May 4 '16 at 7:34
7
$\begingroup$

Just an alternative:

Select[
    list,
    4 <= Tr @ KeyValueMap[# Length[#2] &, #[[-1]] ] <= 12 &
]
| improve this answer | | | | |
$\endgroup$
  • $\begingroup$ There I go, always trying to reinvent the built-in functions. Also it always confuses me to see someone take the trace of a vector, is it just that Tr is shorter than Total? $\endgroup$ – Jason B. May 4 '16 at 7:55
  • $\begingroup$ @JasonB yes, I don't have any other reason. Maybe there are some performance differences but I don't remember :) $\endgroup$ – Kuba May 4 '16 at 7:59
3
$\begingroup$

I made a function that acts on an association list to generate the sum of products you want:

assocTotal = Function[assoc, Total[(# Length@assoc[#] &) /@ (Keys@assoc)]];

When you act on an association list,

assocTotal[<|1 -> {6, 8, 14, 18, 20}, 2 -> {12}, 4 -> {15}|>]
(* 11 *)

Now use Select to get the sublist you are looking for,

Select[list, (4 <= assocTotal[#[[4]]] <= 12 &)]
(* {{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}|>}, {4236, 
  0.103937, 
  0.188204, <|1 -> {5, 6, 8, 14, 18, 20}, 5 -> {12}|>}, {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}|>}, {4966, 
  0.0933867, 
  0.173685, <|2 -> {3}, 1 -> {5, 7, 10, 18, 20}, 5 -> {12}|>}, {5186, 
  0.0923044, 
  0.169445, <|2 -> {4, 5, 11, 12}, 1 -> {6, 18, 20}|>}, {5441, 
  0.0917546, 
  0.167778, <|1 -> {2, 7, 18, 20}, 3 -> {3, 12}, 2 -> {11}|>}, {5661, 
  0.0895339, 
  0.165808, <|1 -> {3, 10, 13, 15, 18, 20}, 5 -> {5}|>}, {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}|>}, {6136, 0.0837377, 
  0.157748, <|6 -> {4}, 1 -> {5, 10, 11, 18, 20}|>}} *)
| improve this answer | | | | |
$\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.