How to get every other item in a list

I have a list in Mathematica, and I'm trying to get all the other numbers in the list and save it as a new list.

I currently have

ReadList["file",Number] 

which reads the entire list, { x1, x2, x3, x4, ... } ; I just want to select any other number and save it in a new list, for example. { x1, x3, x5, ... } .

How to do it?

+6
source share
3 answers

Try:

  yourlist = {a, b, c, d, e, f, g, h}; (* use Span: search for Span or ;; in Documentation Center *) everyotheritemlist = yourlist[[1 ;; -1 ;; 2]]; (* or use Take *) Take[yourlist, {1, -1, 2}] 

Both give:

  {a,c,e,g} 
+8
source

For such tasks, there are always dozens of creative ways to do this in Mathematica. kguler has already given you canonical ways, but here's another one:

 Partition[yourlist, 2]\[Transpose][[1]] (* ==> {a, c, e, g} *) 

By the way: There is a Mathematica Stackexchange website at https://mathematica.stackexchange.com/ . The Mathematica community is moving more and more in this direction, so you can also join us.

+1
source

Another way:

 First /@ ReadList["test.dat", {Number, Number}] 
+1
source

Source: https://habr.com/ru/post/908095/


All Articles