Ask Your Question

refresh numpy array in a for-cycle

asked 2013-06-26 19:36:41 +0200

mresimulator gravatar image

updated 2013-06-27 00:18:21 +0200

ppurka gravatar image

Hi experts!

Im writing a code with a numpy array L, the numpy matrix M and the next script:

for x in L:
    for l in srange(N):
        z= l in L                        
        if z is False and M[x,l] != 0:   

here, in the end of the cycle, new elements are incorporated to the array 'L'.

I want these new elements be considered as 'x' index in the cycle.

When I execute the script I see that only the 'originals' elements of L are considered as 'x'.

How can i fix it?

Waiting for your answers.

Thanks a lot!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-06-30 17:03:15 +0200

vdelecroix gravatar image

You may use a while loop instead. It is never sane to modify the list you iterate on inside a for loop.

i = 0
while i < len(L):
    x = L[i]
    for l in srange(N):
        if not (l in L) and M[x,l] != 0:   
            L = np.append(L,l)

Comment: the test "l in L" might be expensive (if the answer is False then you have to look through the whole list).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-06-26 19:36:41 +0200

Seen: 350 times

Last updated: Jun 30 '13