iteration

Converting a List of Tuples into a Dict in Python

Hi, I have a list of tuples like this: [ ('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('c', 1), ] I want to iterate through this keying by the first item, so for example I could print something like this: a 1 2 3 b 1 2 c 1 How would I go about doing this without keeping an item to track whether the first item is the same as I...

modern for loop for primitive array

Hi, Is there any performance difference between the for loops for primitive array? Assume double[] doubleArray = new double[300000]; for (double var: doubleArray) someComplexCalculation(var); or for ( int i = 0, y = doubleArray.length; i < y; i++) someComplexCalculation(doubleArray[i]); Test result I actually went and...

How to modify or delete items from an enumerable collection while iterating through it in C#

I have to delete some rows from a data table. I've heard that it is not ok to change a collection while iterating through it. So instead of a for loop in which I check if a row meets the demands for deletion and then mark it as deleted, I should first iterate through the data table and add all of the rows in a list, then iterate through ...

Existing LINQ extension method similar to Parallel.For?

The linq extension methods for ienumerable are very handy ... but not that useful if all you want to do is apply some computation to each item in the enumeration without returning anything. So I was wondering if perhaps I was just missing the right method, or if it truly doesn't exist as I'd rather use a built-in version if it's availab...

iterative version of easy recursive algorithm

I have a quite simple question, I think. I've got this problem, which can be solved very easily with a recursive function, but which I wasn't able to solve iteratively. Suppose you have any boolean matrix, like: M: 111011111110 110111111100 001111111101 100111111101 110011111001 111111110011 111111100111 111110001111 I know this is ...

Iterating over vector and calling functions

I have a class that has a vector of another class objects as a member. In many functions of this class I have to do same operation on all the objects in the vector: class Small { public: void foo(); void bar(int x); // and many more functions }; class Big { public: void foo() { for (size_t i = 0; i < V...

Using findAll Collection Closure in Groovy

I have a "Set" that I need to use the findAll closure upon. The Set contains objects, not just primitive values. For instance... I have a Set of Employee objects and I need to iterate and grab elements of that Set of Empolyee Objects by attributes of the Employee. For some reason the findAll closure seems to be just ignore my close and...

Java int[][] array - iterating and finding value

Hi I have an array in the form of 'int[][]' that represents the co-ordinates of a small grid. Each co-ordinate has been assigned its own value. eg array[0][4] = 28...... I have two questions. Firstly, how do I iterate through all the stored values. Secondly, I want to be able to input a value and have its specific co-ordinates in the g...

Recursion or iteration?

I love recursion. I think it simplifies things a lot. Another may disagree; I think it also makes the code a lot easier to read. However, I've noticed that recursion is not used as much in languages such C# as they are in LISP (which by the way is my favorite language because of the recursion). Does anybody know if there is any good re...

How to skip to next iteration in jQuery.each() util?

I'm trying to iterate through an array of elements. jQuery's documentation says: jquery.Each() documentation Returning non-false is the same as a continue statement in a for loop, it will skip immediately to the next iteration. I've tried calling 'return non-false;' and 'non-false;' (sans return) neither of which skip to the next ...

C# Iterating through an enum? (Indexing a System.Array)

I have the following code: // Obtain the string names of all the elements within myEnum String[] names = Enum.GetNames( typeof( myEnum ) ); // Obtain the values of all the elements within myEnum Array values = Enum.GetValues( typeof( myEnum ) ); // Print the names and values to file for ( int i = 0; i < names.Length; i++ ) { pri...

ActionScript2 performance: Iterating over object attributes

Is there a performance hit when iterating over object attributes vs. iterating an array? Example, using objects: var x:Object = {one: 1, two: 2, three: 3}; for (var s:String in x) { trace(x[s]); } Vs using an array var a:Array = [1, 2, 3]; var len:Number = a.length; for (var i:Number = 0; i < len; ++i) { trace(a[i]); } So - whic...

C# Large Tree Iteration

I have a large result set assembled in a parent/child relationship. I need to walk the tree and display the results to the user. I've done this before using recursion, but because my result set may be large, I want to avoid the possibility of receiving a StackOverflowException. I found the following example on MSDN which uses a Stac...

Parallel iteration in C#?

Is there a way to do foreach style iteration over parallel enumerables in C#? For subscriptable lists, I know one could use a regular for loop iterating an int over the index range, but I really prefer foreach to for for a number of reasons. Bonus points if it works in C# 2.0 ...

Iterating over a String (Python)

In C++, I could do: for(int i = 0; i < str.length(); ++i) std::cout << str[i] << std::endl; How do I iterate over a string in Python? ...

Obtaining all possible states of an object for a NP-Complete(?) problem in Python

Not sure that the example (nor the actual usecase) qualifies as NP-Complete, but I'm wondering about the most Pythonic way to do the below assuming that this was the algorithm available. Say you have : class Person: def __init__(self): self.status='unknown' def set(self,value): if value: self.status='happy' else :...

Skipping Iterations in Python

I have a loop going, but there is the possibility for exceptions to be raised inside the loop. Which of course would stop it all together. I want to catch the exceptions and basically just skip to the next iteration in the loop. Is there a keyword or way to do this? ...

Iterating over class properties

I'm trying to iterate over the Color class' Color properties. Unfortunately its not in a collection so its just a class with a bunch of static properties. Does anyone know if its possible to iterate over a class' properties be it static or object based? ...

Reverse iteration through ArrayList gives IndexOutOfBoundsException

When I reverse iterate over an ArrayList I am getting a IndexOutOfBoundsException. I tried doing forward iteration and there is no problem. I expect and know that there are five elements in the list. The code is below: Collection rtns = absRtnMap.values(); List list = new ArrayList(rtns); Collections.sort(list); for(int j=list.size();j...

vb.net cycle through query results

I am familiar with the VB6 ADO way of dealing with SQL queries and looping through the record set results. However, what is the correct way to query a server, cycle through the results, and dispose of my query in VB.Net? All the ways I have been using seem to be unstable and crash randomly. I have been using the following code: Public...