For example, given a list 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8
and a number 4, it returns a list of list with length of 4, that is
(1, 2, 3, 4), (5, 6, 7, 8), (1, 2, 3, 4), (5, 6, 7, 8)
.
Basically I want to implement the following Python code in Powershell.
s = 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8
z = zip(*[iter(s)]*4) # Here N is 4
# z is (1, 2, 3, 4), (5, 6, 7, 8), (1, 2, 3, 4), (5, 6, 7, 8)
The following script returns 17 instead of 5.
$a = 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,0
$b = 0..($a.Length / 4) | % { @($a[($_*4)..($_*4 + 4 - 1)]) }
$b.Length
$n = 4
, and replace all the 4s with$n
should work.0..($a.Length / 4)
instead of1..($a.Length /4)
. So basically it added one more group. There will be an empty array if the length is divided by 4.