当前位置: 动力学知识库 > 问答 > 编程问答 >

python - generate a coordinate system with 2 numbers

问题描述:

The user gives me 2 numbers and I want to take the 2 numbers and generate a grid with coordinates. For example, the user gives me 4 for the first number and 3 for the second, I now have a 4 by 3 grid. How do I generate a coordinate system so that if I wanted, I could take the coordinate (2,2) and compare it with another coordinate (1,1). I'm a beginner just now learning if statements so I don't want to use any weird built ins or high level programming. THANKS!

网友答案:

Do you mean something like this:

def co(N,M):
    print 'Matrix: %s X %s' % (N,M)
    for ind,elm in enumerate(range(N) for x in range(M)):
        print zip([ind]*N, elm)

for i in [(4,5), (5,4), (5,5)]:
    co(i[0],i[1])

Output:

Matrix: 4 X 5
[(0, 0), (0, 1), (0, 2), (0, 3)]
[(1, 0), (1, 1), (1, 2), (1, 3)]
[(2, 0), (2, 1), (2, 2), (2, 3)]
[(3, 0), (3, 1), (3, 2), (3, 3)]
[(4, 0), (4, 1), (4, 2), (4, 3)]

Matrix: 5 X 4
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4)]
[(1, 0), (1, 1), (1, 2), (1, 3), (1, 4)]
[(2, 0), (2, 1), (2, 2), (2, 3), (2, 4)]
[(3, 0), (3, 1), (3, 2), (3, 3), (3, 4)]

Matrix: 5 X 5
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4)]
[(1, 0), (1, 1), (1, 2), (1, 3), (1, 4)]
[(2, 0), (2, 1), (2, 2), (2, 3), (2, 4)]
[(3, 0), (3, 1), (3, 2), (3, 3), (3, 4)]
[(4, 0), (4, 1), (4, 2), (4, 3), (4, 4)]
分享给朋友:
您可能感兴趣的文章:
随机阅读: