Ask Your Question
1

refresh numpy array in a for-cycle

asked 11 years ago

mresimulator gravatar image

updated 11 years ago

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:   
            L=np.append(L,l)

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!

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
2

answered 11 years ago

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).

Preview: (hide)
link

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 11 years ago

Seen: 415 times

Last updated: Jun 30 '13