开发者

problem with arrays in python ,help!

I am making a program in python and I am having an error that I cannot solve.

This is the problem:

I have a set to points in 3D space, and I am storing it in a vector(rake). My point is to build a stream surface. So I am appending those points to another list so that I can have all the points from the "line" before.

The rake list has this format:

[[60, 0, 50], [63, 3, 50], [66, 6, 50], [69, 9, 50], [72, 12, 50], 
 [75, 15, 50], [78, 18, 50], [81, 21, 50], [84, 24, 50], [87, 27, 50], 
 [90, 30, 50], [93, 33, 50], [96, 36, 50], [99, 39, 50], [102, 42, 50]]

Then when I append the points to the other list(points_list) is like this:

[[[60, 0, 50], [63, 3, 50], [66, 6, 50], [69, 9, 50], [72, 12, 50], 
 [75, 15, 50], [78, 18, 50], [81, 21, 50], [84, 24, 50], [87, 27, 50], 
 [90, 30, 50], [93, 33, 50], [96, 36, 50], [99, 39, 50], [102, 42, 50]]]

My point is that with the points_list I can know in witch iteration level I am dealing with, so that I could render the surface in the end.

When I try to get, for instance, one element from the points_arrays I have and index error. this is the code:

points_arrays.append(rake)

for i in range(iterations):     
  for j in range(rlength):    
    print points_arrays[i][j][0],points_arrays[i][j][1],points_arrays[i][j][1]

When I run this part of the code I am able to get the points but in the end I get an index error. (IndexError: list index out of range)

Can anyone help me to solve t开发者_开发技巧his??


Your main problem is that you should use extend instead of append:

points_list.extend(rake)

This is because append adds a single object to the end of the list. In this case it means that the entire second list is appended as a single element.

append - append object to end
extend - extend list by appending elements from the iterable

You should also be aware of the following points that are not directly related to your problem:

  • In Python the object created when you write [1, 2, 3] is called a list, not an array.
  • Your print statement is wrong. The second occurrence of points_arrays[i][j][1] should be points_arrays[i][j][2]


for rake in points_list:
  for point in rake:
    print point[0], point[1], point[2]

If you want to use numbers as indexes:

for npoint in xrange(len(points_list))
  for nrake in xrange(len(points_list[npoint]))
    print points_list[npoint][nrake][0], points_list[npoint][nrake][1], points_list[npoint][nrake][2]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜