For a given list, say {94,35,47,29,86,40,46,72,87}, how can I find a list of even and odd numbers.
Here is the output after trying with the suggested solution in the answer.
After putting the number in list as told by @Nasser, I got the intended output.
I am seeking to solve this problem:
Create an Association that lists the Median of the even and odd numbers respectively under the keys True and False. (Hint: Use GroupBy with EvenQ.)
By applying GroupBy, I could now list the even and odd numbers:
Input:
GroupBy[{94,35,47,29,86,40,46,72,87,84,49,82,100,99,7,70,67,63,13,
91,18,94,94,15,72,39,4,7,31,67,65,37,93,9,4,84,97,60,48,61,91,24,10,
46,42,28,41,36,62,70,78,38,80,70,63,20,21,7,54,26,17,73,100,56,36,
54,100,46,0,59,77,80,57,82,63,74,8,47,68,74,54,57,4,90,14,31,88,23,
30,37,95,44,100,12,23,48,4,14,100,57},EvenQ]
Output:
True-> {94,86,40,46,72,84,82,100,70,18,94,94,72,4,4,84,60,48,24,10,46,
42,28,36,62,70,78,38,80,70,20,54,26,100,56,36,54,100,46,0,80,
82,74,8,68,74,54,4,90,14,88,30,44,100,12,48,4,14,100},
False-> {35,47,29,87,49,99,7,67,63,13,91,15,39,7,31,67,65,37,93,9,97,
61,91,41,63,21,7,17,73,59,77,57,63,47,57,31,23,37,95,23,57}
What is needed is the count. True->54, False->49.
With CountsBy as suggested by @sjoerd-smit, I get the count of even and odd numbers in one command.
CountsBy[{94,35,47,29,86,40,46,72,87,84,49,82,100,99,7,70,67,63,13,
91,18,94,94,15,72,39,4,7,31,67,65,37,93,9,4,84,97,60,48,61,91,24,10,
46,42,28,41,36,62,70,78,38,80,70,63,20,21,7,54,26,17,73,100,56,36,
54,100,46,0,59,77,80,57,82,63,74,8,47,68,74,54,57,4,90,14,31,88,23,
30,37,95,44,100,12,23,48,4,14,100,57},EvenQ]
By Median applied individually, I could get the medians of even and odd numbers in the list.
Input:
Median[{35,47,29,87,49,99,7,67,63,13,91,15,39,7,31,67,65,37,93,
9,97,61,91,41,63,21,7,17,73,59,77,57,63,47,57,31,23,37,95,23,57}]
Output: 49
Similarly, for the list of even numbers.
Might be a way to get the same with one command.
GroupBy
overGatherBy
, to be honest. It's just a more powerful function andGatherBy
can be unpredictable in the ordering of the sublists. $\endgroup$GroupBy[{94,35,...},EvenQ,Median]
( <|True -> 54, False -> 49|> ) or if you want both Counts and Median, maybeGroupBy[{94,35,...},EvenQ,#]&/@{Length,Median}
( {<|True -> 59, False -> 41|>, <|True -> 54, False -> 49|>} ). But perhaps you require something else? $\endgroup$