iterator

Python 2: someIterator.next() vs. next(someIterator) :Python 3

老子叫甜甜 提交于 2020-08-19 08:17:13
问题 In Python 2 iterators offer .next() , a callable method: it = iter(xrange(10)) it.next() > 0 it.next() > 1 ... In Python 3 one has to use the built-in function next() : it = iter(range(10)) next(it) > 0 next(it) > 1 ... Is this just "syntactic sugar"? Like making it more obvious to use next() by moving it into the built-in functions? Or does any advanced concept hide behind this change? 回答1: You are directly asking about PEP 3114 consider the following code: class test: def __iter__(self):

Lifetime parameter problem in custom iterator over mutable references

寵の児 提交于 2020-08-10 20:00:31
问题 I'd like to implement a custom Iterator like below, but cannot solve reference problem. use itertools::Product; use std::ops::Range; struct Iter2DMut<'a, T: 'a> { data: &'a mut [T], shape: (usize, usize), idx_iter: Product<Range<usize>, Range<usize>>, } impl<'a, T: 'a> Iterator for Iter2DMut<'a, T> { type Item = &'a mut T; fn next(&mut self) -> Option<Self::Item> { if let Some((i, j)) = self.idx_iter.next() { Some(&mut self.data[i + self.shape.0 * j]) } else { None } } } and get the following

Retrieve an arbitrary key from python3 dict in O(1) time

╄→гoц情女王★ 提交于 2020-08-10 02:00:56
问题 I need to retrieve an arbitrary key from a python dictionary object. Suppose I have a dictionary d . What's the time complexity of the following code? k = next(iter(d.keys())) I get that d.key() is in O(1) time in Python 3, next() is in O(1) time. What happens to the iter()? Can this be done in O(1) time without using extra space? Thanks! 回答1: Using iter (or other logic, such as a generator expression, declaring a generator function which delegates to the dict, using islice , etc..) are all

Retrieve an arbitrary key from python3 dict in O(1) time

拥有回忆 提交于 2020-08-10 02:00:29
问题 I need to retrieve an arbitrary key from a python dictionary object. Suppose I have a dictionary d . What's the time complexity of the following code? k = next(iter(d.keys())) I get that d.key() is in O(1) time in Python 3, next() is in O(1) time. What happens to the iter()? Can this be done in O(1) time without using extra space? Thanks! 回答1: Using iter (or other logic, such as a generator expression, declaring a generator function which delegates to the dict, using islice , etc..) are all

Have range-based for loop start at point in vector

ε祈祈猫儿з 提交于 2020-07-23 01:03:14
问题 I have a std::vector<MyClass*> container of pointers to objects of some class, and want to iterate over the vector using a range-based for loop, like this for (MyClass *item : container) { // Do stuff, not changing the container } Inside this loop I want to loop through the container one more time, but starting at the next element in the container. I couldn't really find a way of doing this without iterators, so my solution was to just use those instead for (auto item1 = container.begin();

Have range-based for loop start at point in vector

社会主义新天地 提交于 2020-07-23 01:02:11
问题 I have a std::vector<MyClass*> container of pointers to objects of some class, and want to iterate over the vector using a range-based for loop, like this for (MyClass *item : container) { // Do stuff, not changing the container } Inside this loop I want to loop through the container one more time, but starting at the next element in the container. I couldn't really find a way of doing this without iterators, so my solution was to just use those instead for (auto item1 = container.begin();

Have range-based for loop start at point in vector

戏子无情 提交于 2020-07-23 01:00:45
问题 I have a std::vector<MyClass*> container of pointers to objects of some class, and want to iterate over the vector using a range-based for loop, like this for (MyClass *item : container) { // Do stuff, not changing the container } Inside this loop I want to loop through the container one more time, but starting at the next element in the container. I couldn't really find a way of doing this without iterators, so my solution was to just use those instead for (auto item1 = container.begin();

How to implement standard iterators in class

好久不见. 提交于 2020-07-18 11:28:18
问题 I have classes which are usually using standard containers as underlying fields. For example, I have a class template <typename T> class Vec_3D { public: /* ... */ std::array<T, 3> vec; /* ... */ }; which has only one variable vec and the rest are just functions I need when working with vectors. I want to be able to use range-based for loop such as Vec_3D<double> vec; for (double val : vec) {/*...*/} which should obviusly iterate over std::array<double, 3> . How to implement iterators in my

How to implement standard iterators in class

可紊 提交于 2020-07-18 11:28:06
问题 I have classes which are usually using standard containers as underlying fields. For example, I have a class template <typename T> class Vec_3D { public: /* ... */ std::array<T, 3> vec; /* ... */ }; which has only one variable vec and the rest are just functions I need when working with vectors. I want to be able to use range-based for loop such as Vec_3D<double> vec; for (double val : vec) {/*...*/} which should obviusly iterate over std::array<double, 3> . How to implement iterators in my

Pythonic solution to drop N values from an iterator

匆匆过客 提交于 2020-07-18 10:41:10
问题 Is there a pythonic solution to drop n values from an iterator? You can do this by just discarding n values as follows: def _drop(it, n): for _ in xrange(n): it.next() But this is IMO not as elegant as Python code should be. Is there a better approach I am missing here? 回答1: I believe you are looking for the "consume" recipe http://docs.python.org/library/itertools.html#recipes def consume(iterator, n): "Advance the iterator n-steps ahead. If n is none, consume entirely." # Use functions that