iterator

usage of iterator() on django queryset

橙三吉。 提交于 2020-07-18 03:31:19
问题 I came across some strange behaviour recently, and need to check my understanding. I'm using a simple filter in the model and then iterating over the results. e.g. allbooks = Book.objects.filter(author='A.A. Milne') for book in allbooks: do_something(book) oddly, it was returning only a partial list of books. However, when using the same code and using iterator(), this seems to work well. i.e. for book in allbooks.iterator(): do_something(book) Any idea why? p.s. I did look through the Django

How to correctly create std::string from a std::string_view?

你离开我真会死。 提交于 2020-07-09 02:47:19
问题 I have a class: class Symbol_t { public: Symbol_t( const char* rawName ) { memcpy( m_V, rawName, 6 * sizeof( char ) ); }; string_view strVw() const { return string_view( m_V, 6 ); }; private: char m_V[6]; }; // class Symbol_t and there is a lib-func that I can't modify: extern bool loadData( const string& strSymbol ); If there is a local variable: Symbol_t symbol( "123456" ); When I need to call loadData, I dare not do it like this: loadData( string( symbol.strVw().begin(), symbol.strVw().end

Is clamping on iterators valid

妖精的绣舞 提交于 2020-07-08 11:49:02
问题 I found the following in actual production code. My suspicion is that it actually has undefined behavior into it, however, I couldn't find the related info on cppreference. Can you confirm this is UB or valid code and why this is UB/valid (preferably with a quote of the standard)? #include <vector> int main(int, char **) { auto v = std::vector<int>({1,2,3,4,5}); auto begin = v.begin(); auto outOfRange = begin + 10; auto end = v.end(); auto clamped = std::min(outOfRange, end); return (clamped

Duplicate each member in an iterator

时光毁灭记忆、已成空白 提交于 2020-07-05 12:55:50
问题 Given an iterator i , I want an iterator that yields each element n times, i.e., the equivalent of this function def duplicate(i, n): for x in i: for k in range(n): yield x Is there an one-liner for this? Related question: duplicate each member in a list - python, but the zip solution doesn't work here. 回答1: This is my simple solution, if you want to duplicate each element same times. It returns a generator expression, which should be memory efficient. def duplicate(i, n): return (k for k in

How to use an async for loop to iterate over a list?

我是研究僧i 提交于 2020-07-05 01:25:10
问题 So I need to call an async function for all items in a list. This could be a list of URLs and an async function using aiohttp that gets a response back from every URL. Now obviously I cannot do the following: async for url in ['www.google.com', 'www.youtube.com', 'www.aol.com']: I can use a normal for loop but then my code will act synchronously and I lose the benefits and speed of having an async response fetching function. Is there any way I can convert a list such that the above works? I

What could be a “least bad implementation” for an iterator over a proxied container?

江枫思渺然 提交于 2020-06-27 16:28:22
问题 Context I was trying to implement a nD array like container. Something that would wrap an underlying sequence container and allow to process it as a container of containers (of...): arr[i][j][k] should be a (eventually const) reference for _arr[(((i * dim2) + j) * dim3) + k] . Ok until there, arr[i] has just to be a wrapper class over the subarray... And when I tried to implement interators, I suddenly realized that dragons were everywhere around: my container is not a standard compliant

Python iter() time complexity?

核能气质少年 提交于 2020-06-27 09:56:27
问题 I was looking up an efficient way to retrieve an (any) element from a set in Python and came across this method: anyElement = next(iter(SET)) What exactly happens when you generate an iterator out of a container such as a set? Does it simply create a pointer to the location of the object in memory and move that pointer whenever next is called? Or does it convert the set to a list then create an iterator out of that? My main concern is if it were the latter, it seems iter() would be an O(n)

Counting average on list<T> field

丶灬走出姿态 提交于 2020-06-27 06:51:30
问题 I have list of A, and I want to count average on it's field a. What's the best way to do it? class A { int a; int b; } void f() { var L = new List<A>(); for (int i=0; i<3; i++) { L.Add(new A(){a = i}); } } 回答1: Enumerable.Average has an overload that takes a Func<T, int> as an argument. using System.Linq; list.Average(item => item.a); 回答2: You could try this one: var average = ListOfA.Select(x=>x.a).Average(); where ListOfA is a List of objects of type A . 回答3: You can use Enumerable.Average

Bug in Scala 2.10, Iterator.size?

回眸只為那壹抹淺笑 提交于 2020-06-25 09:45:09
问题 Is this normal? scala> val x = Iterator(List[String]("str")) lol: Iterator[List[String]] = non-empty iterator scala> x.size res1: Int = 1 scala> x.size res2: Int = 0 And actually I'm meeting other weird errors.. a possible bug? 回答1: No it's not a bug. It's the normal behavior. Iterators are mutable things. You can think of them as pointers. Each time you ask an iterator to give you the next element it points to it will move one position further. When you ask it to give you the size it will

Fastest (most Pythonic) way to consume an iterator

泪湿孤枕 提交于 2020-06-24 21:43:52
问题 I am curious what the fastest way to consume an iterator would be, and the most Pythonic way. For example, say that I want to create an iterator with the map builtin that accumulates something as a side-effect. I don't actually care about the result of the map , just the side effect, so I want to blow through the iteration with as little overhead or boilerplate as possible. Something like: my_set = set() my_map = map(lambda x, y: my_set.add((x, y)), my_x, my_y) In this example, I just want to