简体   繁体   中英

missing items from python lists

I have two functions that generates two matrices, the first function generate some square matrix, while the second one generate a 1D matrix depending on the first matrix. Both functions depend on parameter N, the second function matrix result should have a matrix with (N*2)**2 .My problem is when i use a value of the parameter N grater than 4 their is always 10 missing elements from the second function result.(The OP functions have been deleted to create a minimal question. Check the edit for details. An example function is below demonstrating the issue

def test(N):
        m = []
        for i in range(0,(N*2)):
            for j in range(-1,(N*7),4):
                m.append((i,j))


        return len(m)

example for N = 4

test(4)

Output:
64

example for N = 5

test(5)

Output:
90

The result in example 2 is 90 while it should be 100, can anyone help please

You can easily check how many iterations either range will cause:

>>> n = 5
>>> len(range(0, n*2))
10
>>> len(range(-1, n*7, 4))
9

10*9 is indeed a total of 90 iterations and 90 elements added to the array.

I'm assuming you expect more elements from the inner loop, so probably you are confused about how range treats boundaries. The quick solution is to try variations like n*7+1 until you get the correct behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM