What is the idiomatic way to iterate over a binary file?

Try: chunk_size = 4 * 1024 * 1024 # MB with open(‘large_file.dat’,’rb’) as f: for chunk in iter(lambda: f.read(chunk_size), b”): handle(chunk) iter needs a function with zero arguments. a plain f.read would read the whole file, since the size parameter is missing; f.read(1024) means call a function and pass its return value (data loaded from … Read more

How to create a copy of python iterator? [duplicate]

Use the itertools.tee() function to produce copies; these use a buffer to share results between different iterators: from itertools import tee my_list = [5, 4, 3,2] first_it = iter(my_list) first_it, second_it = tee(first_it) print next(first_it) # prints 5 print next(second_it) # prints 5 print next(first_it) # prints 4 Note that you should no longer use … Read more

Join iterator of &str

You could use the itertools crate for that. I use the intersperse helper in the example, it is pretty much the join equivalent for iterators. cloned() is needed to convert &&str items to &str items, it is not doing any allocations. It can be eventually replaced by copied() when [email protected] gets a stable release. use … Read more

Class accessible by iterator and index

The current accepted answer from @Ignacio Vazquez-Abrams is sufficient. However, others interested in this question may want to consider inheriting their class from an abstract base class (ABC) (such as those found in the standard module collections.abc). This does a number of things (there are probably others as well): ensures that all of the methods … Read more

iterator for 2d vector

Although your question is not very clear, I’m going to assume you mean a 2D vector to mean a vector of vectors: vector< vector<int> > vvi; Then you need to use two iterators to traverse it, the first the iterator of the “rows”, the second the iterators of the “columns” in that “row”: //assuming you … Read more

Shortest way to get an Iterator over a range of Integers in Java

This implementation does not have a memory footprint. /** * @param begin inclusive * @param end exclusive * @return list of integers from begin to end */ public static List<Integer> range(final int begin, final int end) { return new AbstractList<Integer>() { @Override public Integer get(int index) { return begin + index; } @Override public int … Read more

python: iterating through a dictionary with list values

Here’s a speed test, why not: import random numEntries = 1000000 d = dict(zip(range(numEntries), [random.sample(range(0, 100), 2) for x in range(numEntries)])) def m1(d): for k in d: for x in d[k]: pass def m2(d): for k, dk in d.iteritems(): for x in dk: pass import cProfile cProfile.run(‘m1(d)’) print cProfile.run(‘m2(d)’) # Ran 3 trials: # m1: … Read more

Making a python iterator go backwards?

No, in general you cannot make a Python iterator go backwards. However, if you only want to step back once, you can try something like this: def str(self, item): print item prev, current = None, self.__iter.next() while isinstance(current, int): print current prev, current = current, self.__iter.next() You can then access the previous element any time … Read more