Iterate an Array, N Items at a Time

Iterate an array, n items at a time

You are looking for #each_slice.

data.each_slice(3) {|slice| ... }

Python Iterating n items of array at a time

You can split the list into slices to iterate over.

l = [ ... ]
for x in (l[i:i + 50] for i in range(0, len(l), 50)):
print(x)

Loop through an array by set of 4 elements at a time in Javascript?

Use a for loop where i increases by 4.

Note: I was working on my answer when Jaromanda X commented the same thing.

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
for (var i = 0; i < arr.length; i += 4) { console.log("Working with: " + arr.slice(i, i + 4));}

Iterate over an array n items at a time and continue the iteration

Ruby has an Enumerable#each_slice method that will give you an array in groups, which could allow you to do something similar to:

my_arr = my_arr.collect.with_index do |my_obj, index|
"#{index} #{my_obj.name}" # do this all the way up here to get the original index
end.each_slice(5)

length = my_arr.size - 1 # how many groups do we need to display
my_arr.each.with_index do |group, index|
puts group.join("\n") # show the group, which is already in the desired format

if index < length # if there are more groups to show,
# show a message and wait for input
puts "-- MORE --"
gets
end
end

Iterate an array n times at a time in php

Split the array,

$newarray = array_chunk($array, 4);

Now $newarray has array of arrays having 4 items in each. like this,

$newarray = Array(
0 => Array(
0 => "ed",
1 => "smith.co.uk",
2 => "http://edsmith.co.uk/smith.jpg",
3 => "Published"
),
1 => Array(
0 => "ford attenborough",
1 => "ford.co.uk",
2 => "http://fordattenborough.co.uk/ford.jpg",
3 => "Pending Approval"
),
2 => Array(
0 => "greg tolkenworth",
1 => "greg.co.uk",
2 => "http://greg.co.uk/greg.jpg",
3 => "In Future"
)
);

Now you can run a foreach on $newarray to print 4 items at a time.

foreach($newarray as $arr){
foreach($arr as $a){
echo $a;
}
}

Loop 'n' elements in groups in each iteration

Use plain old for loop, like this

var N = 3;
for (var i = 0; i < array.length; i += N) {
// Do your work with array[i], array[i+1]...array[i+N-1]
}

How to iterate a loop every n items

You can also do this with LINQ:

var largeList = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
for (int i = 0; i < largeList.Count; i += 3)
{
var items = largeList.Skip(i).Take(3).ToList();
// do stuff with your 3 (or less items)
}

Python how to iterate over a list 100 elements at a time until I reach all elements?

Trying to keep it simple because I assume you are starting with Python

Iterate the list increasing a hundred every iteration

my_list = [i for i in range(10123)]
for i in range(0, len(my_list), 100):
process_list(my_list[i:i+100])

def process_list(my_list)
url = 'https://api.example.com'
data = {'update_list': my_list}
headers = {'auth': auth}
r = requests.put(url, data=json.dumps(data), headers=headers)

You have two options on how to use range from the docs:

range(start, stop[, step])

or

range(stop)

Using the first option you iterate through the sequence 0, 100, 200, ...

Iteration through an array reads n-1 elements from array

Let's look at your loop. It overwrites test at every iteration, so you only really need to look the last value of i.

You're using a range from 5 inclusive, to randnums.size == 100 (exclusive). Ranges are exclusive on the upper bound. The last element of the range is 99. You can check this by printing it directly (ranges are sequences):

>>> print(range(5, randnums.size)[-1])
99

Your code is therefore equivalent to

test = randnums[:99]
print(test.size)

The result should not be unexpected at this point.



Related Topics



Leave a reply



Submit