simont |
Wed 2011-12-07 16:31 |
it looks naïvely as though the viewitems version would take O(n) time
The Python help says that if D is a dictionary then D.viewitems() returns "a set-like object providing a view on D's items", which seems to me like a clear hint that it's not going to the effort of physically constructing an actual Python set object but is instead providing a duck-typed thing that behaves like a set. So I would expect it to DTRT, performance-wise.
In C++, what you want is relatively straightforward:
It's straightforward if you're happy to use a whole statement and a variable declaration! All of the Python idioms I suggest above, including the two-clause one that seems to me most analogous to your C++ version, can be used in the middle of a larger expression without having to stop to declare local variables first. |
|