What is the difference between chain and chain.from_iterable in itertools?

The first takes 0 or more arguments, each an iterable, the second one takes one argument which is expected to produce the iterables: from itertools import chain chain(list1, list2, list3) iterables = [list1, list2, list3] chain.from_iterable(iterables) but iterables can be any iterator that yields the iterables: def gen_iterables(): for i in range(10): yield range(i) itertools.chain.from_iterable(gen_iterables()) … Read more

Vector erase iterator

res.erase(it) always returns the next valid iterator, if you erase the last element it will point to .end() At the end of the loop ++it is always called, so you increment .end() which is not allowed. Simply checking for .end() still leaves a bug though, as you always skip an element on every iteration (it … Read more

What is the purpose/advantage of using yield return iterators in C#?

But what if you were building a collection yourself? In general, iterators can be used to lazily generate a sequence of objects. For example Enumerable.Range method does not have any kind of collection internally. It just generates the next number on demand. There are many uses to this lazy sequence generation using a state machine. … Read more

How to iterate through a list of objects in C++?

You’re close. std::list<Student>::iterator it; for (it = data.begin(); it != data.end(); ++it){ std::cout << it->name; } Note that you can define it inside the for loop: for (std::list<Student>::iterator it = data.begin(); it != data.end(); ++it){ std::cout << it->name; } And if you are using C++11 then you can use a range-based for loop instead: for … Read more

“On-line” (iterator) algorithms for estimating statistical median, mode, skewness, kurtosis?

I use these incremental/recursive mean and median estimators, which both use constant storage: mean += eta * (sample – mean) median += eta * sgn(sample – median) where eta is a small learning rate parameter (e.g. 0.001), and sgn() is the signum function which returns one of {-1, 0, 1}. (Use a constant eta if … Read more

Is there a Java equivalent of Python’s ‘enumerate’ function?

For collections that implement the List interface, you can call the listIterator() method to get a ListIterator. The iterator has (amongst others) two methods – nextIndex(), to get the index; and next(), to get the value (like other iterators). So a Java equivalent of the Python above might be: import java.util.ListIterator; import java.util.List; List<String> numbers … Read more