3
$\begingroup$

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.

enter image description here

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.

$\endgroup$
6
  • 5
    $\begingroup$ Please take a look at GatherBy $\endgroup$
    – Syed
    Sep 3 at 6:36
  • 2
    $\begingroup$ @Syed I recommend always using GroupBy over GatherBy, to be honest. It's just a more powerful function and GatherBy can be unpredictable in the ordering of the sublists. $\endgroup$ Sep 3 at 8:05
  • 4
    $\begingroup$ Dear OP, please start a fresh question for new queries, instead of morphing the question. $\endgroup$
    – Syed
    Sep 3 at 8:40
  • $\begingroup$ @Syed Did not start a new question as from my previous experience, I was told to remove the first question as I formulated the second question based on input from the first one. $\endgroup$ Sep 4 at 5:43
  • 1
    $\begingroup$ The problem I have is that I am no longer sure what you are asking. But maybe you want GroupBy[{94,35,...},EvenQ,Median] ( <|True -> 54, False -> 49|> ) or if you want both Counts and Median, maybe GroupBy[{94,35,...},EvenQ,#]&/@{Length,Median} ( {<|True -> 59, False -> 41|>, <|True -> 54, False -> 49|>} ). But perhaps you require something else? $\endgroup$
    – user1066
    Sep 4 at 8:21
8
$\begingroup$

If you only need the counts, then CountsBy is the way to go:

CountsBy[Range[100], EvenQ]

You can also do it with the 3rd argument of GroupBy:

GroupBy[Range[100], EvenQ, Length]

If you want the results in a list in a particular order (say, even then odd), you can use Lookup:

Lookup[CountsBy[Range[100], EvenQ], {True, False}, 0]
$\endgroup$
7
$\begingroup$

I think this was asked before, but I can't find it now searching.

How about

lis = {94, 35, 47, 29, 86, 40, 46, 72, 87}
{Select[lis, EvenQ[#] &], Select[lis, OddQ[#] &]}

Mathematica graphics

I am sure there are many other ways to do this.

edit:

A little simpler version as suggested in comment thanks to kglr is

{Select[lis, EvenQ], Select[lis, OddQ]}
$\endgroup$
3
  • 3
    $\begingroup$ you can use simpler Select[lis, EvenQ] $\endgroup$
    – kglr
    Sep 3 at 6:54
  • $\begingroup$ Getting the first few elements in black (attached image with the question). Is it due to some mismatch with global variables? $\endgroup$ Sep 3 at 7:21
  • 2
    $\begingroup$ @SplendidDigitalSolutions you need to put the numbers in a list first. a list is { ....} then it will work. You can't just list the number as you did. $\endgroup$
    – Nasser
    Sep 3 at 7:28
2
$\begingroup$

Try Cases:

list={94, 35, 47, 29, 86, 40, 46, 72, 87};
twolists=List[Cases[list, Except[_?EvenQ]], Cases[list, Except[_?OddQ]]]
(*{{35, 47, 29, 87}, {94, 86, 40, 46, 72}}*)
Map[Median, twolists]
(*{41,72}*)

With order:

List[Sort[Cases[list, Except[_?EvenQ]], Less], Sort[Cases[list, Except[_?OddQ]], Less]]
(*{{29, 35, 47, 87}, {40, 46, 72, 86, 94}}*)
List[Sort[Cases[list, Except[_?EvenQ]], Greater], Sort[Cases[list, Except[_?OddQ]], Greater]]
(*{{87, 47, 35, 29}, {94, 86, 72, 46, 40}}*)
$\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.