How to execute two “aggregate” functions (like sum) concurrently, feeding them from the same iterator?

Let’s consider how to apply two aggregate functions to the same iterator, which we can only exhaust once. The initial attempt (which hardcodes sum and max for brevity, but is trivially generalizable to an arbitrary number of aggregate functions) might look like this: def max_and_sum_buffer(it): content = list(it) p = sum(content) m = max(content) return … Read more

Why is an iterable object not an iterator?

The concept of iteration is well documented in the Python documentation. In short, “iterable” is the object which provides the items I want to iterate over. Either it already cintains these items, then it is also called the container. This can be a list, a string, a tuple or anything else which consists of zero … Read more

Algorithm for implementing C# yield statement

The particular code sample you are looking at involves a series of transformations. Please note that this is an approximate description of the algorithm. The actual names used by the compiler and the exact code it generates may be different. The idea is the same, however. The first transformation is the “foreach” transformation, which transforms … Read more

How to iterate 1d NumPy array with index and value [duplicate]

There are a few alternatives. The below assumes you are iterating over a 1d NumPy array. Iterate with range for j in range(theta.shape[0]): # or range(len(theta)) some_function(j, theta[j], theta) Note this is the only of the 3 solutions which will work with numba. This is noteworthy since iterating over a NumPy array explicitly is usually … Read more