How to make C ++ style (indexed) by nested loops in python?

What is the equivalent of the following in python?

for (i=0; i<n; i++) for (j=i+1; j<n; j++) //do stuff with A[i], A[j] 

Or in a sense the following. He must also remove an element from A at the end of each round of the cycle.

 for a in A: for a' in A/{a}: #ie rest of the elements of A #do something with a,a' #remove a from A 

Is there a pythonic way to do this without using enumerate ()?

Changes:

Sorry for the poor description.

  • In the first example, I want to use i and j only as indices. Their values ​​do not matter. Its just the crude equivalent of C ++ of the latter.

  • The outer loop runs n times. The inner loop executes (n-1), (n-2) ... 0 times for each iteration of the outer loop.

Maybe this might help (pseudo code):

 function next_iteration(list): head = first element tail = remaining elements #list each element in tail interacts with head one by one next_iteration(tail) 

PS: All of the above code examples are pseudo-codes. I'm trying to express something that is still a little vague in my mind.

+4
source share
11 answers

Since your two questions are different, here is a solution for your second problem:

 for i in xrange(len(A)): for j in xrange(len(A)): if i != j: do_stuff(A[i], A[j]) 

or using itertools (I think that using the included batteries is very pythonic!):

 import itertools for a, b in itertools.permutations(A, 2): do_stuff(a, b) 

This will apply do_stuff to all combinations of 2 different elements from A. I want to save the result simply by using:

 [do_stuff(a, b) for a, b in itertools.permutations(A, 2)] 
+3
source

I suggest that you ask how

How can I iterate over all pairs of individual container elements?

Answer:

 >>> x = {1,2,3} >>> import itertools >>> for a, b in itertools.permutations(x, 2): ... print a, b ... 1 2 1 3 2 1 2 3 3 1 3 2 

EDIT: If you don't want to use both (a,b) and (b,a) , just use itertools.combinations .

+4
source

What about:

 for i in range(0,n): for j in range (i+1,n): # do stuff 
0
source
 for i in range(0,n): for j in range(i+1,n): # do stuff 
0
source

Still you can’t leave comments .. but basically what the other two posts said, but used to using xrange instead of range.

 for i in xrange(0,n): for j in xrange(i+1,n): # do stuff 
0
source

You can use xrange to generate values ​​for i and j respectively, as shown below:

 for i in xrange(0, n): for j in xrange(i + 1, n): # do stuff 
0
source

You can make an inner loop directly above the slice. Not to say it's better, but it's a different approach.

 for i in range(0,len(x)): a = x[i] for b in x[i+1:]: print a, b 
0
source

Another way to approach this is if n is a sequence that provides an iterable interface, then in Python you can simplify your code by iterating through an object directly:

 for i in n: for some_var in n[n.index(i):]: # rest of items # do something 

I hope I understood your loop correctly, because others have stated that they do not do the same.

0
source

For the first of your questions, as mentioned in other answers:

 for i in xrange(n): for j in xrange(i+1, n): # do stuff with A[i] and A[j] 

For the second:

 for i, a in enumerate(A): for b in A[i+1:]: # do stuff with a and b 
0
source

Your psuedocode almost has it:

 function next_iteration(list): head = first element tail = remaining elements #list each element in tail interacts with head one by one next_iteration(tail) 

Python Code:

 def next_iteration(lst): head, tail = lst[0], lst[1:] for item in tail: print(head, item) if tail: next_iteration(tail) 

What when trying to next_iteration([1, 2, 3]) using next_iteration([1, 2, 3]) :

 1 2 1 3 2 3 
0
source

In the first for-loop, enumeration () goes through the array and makes the index, the value of each element available for the second for loop. In the second loop, range () makes j = i + 1 -> len (a) available. At this point, you will have exactly what you need, i and j to complete your operation.

 >>> a = [1,2,3,4] >>> array_len = len(a) >>> for i,v in enumerate(a): ... for j in range(i+1, array_len): ... print a[i], a[j] ... 1 2 1 3 1 4 2 3 2 4 3 4 >>> 
0
source

Source: https://habr.com/ru/post/1402436/


All Articles