I want to drop 2 continuous elements every 3 elements,such as I have a list:
list = {3, 5, 5, 5, 2, 5, 1, 0, 5, 0, 4};
I want to get
{3, 5, 5, 5, 1, 0, 4}
Any consice method can do this?
R.M's method with suitable modification:
Flatten @ Partition[list, 3, 5, 1, {}]
{3, 5, 5, 5, 1, 0, 4}
Or for recent versions of Mathematica using UpTo
Flatten @ Partition[list, UpTo[3], 5]
Partition
can easily do this..
$\endgroup$
UpTo[3]
instead of 3
slows down the solution with a factor 8 / 10. Therefore, for very long lists @rcollyer's solution Pick[list,PadRight[#,Length@list,#]&@UnitStep@Range[2,-2,-1],1]
or this variant Pick[list, Clip[Mod[Range[Length[list]], 5], {2,3}], 2]
could be considered as well.
$\endgroup$
Commented
Mar 14, 2017 at 19:28
Partition
slows it down to begin with, i.e. {}
is slower than doing a PadRight
on the original list then trimming after. I cannot remember where this was discussed but a search would probably find it.
$\endgroup$
Commented
Mar 15, 2017 at 6:34
Flatten[Take[#, UpTo[3]] & /@ Partition[list, 5, 5, 1, {}]]
{3, 5, 5, 5, 1, 0, 4}
Flatten[Take[#,UpTo[3]]&/@Partition[list,UpTo[5]]]
.
$\endgroup$
Commented
Mar 8, 2017 at 18:50
Flatten[Map[list[[# ;; ;; 5]] &, {1, 2, 3}], {2, 1}]
{3, 5, 5, 5, 1, 0, 4}