Python 当我只需要1个内部列表时,循环打印长度为2个内部列表
我有一个xy坐标的列表,如下所示: [xcoords][ycoords]] 因为我的For循环使用整个列表的长度,所以它打印90多个结果,而实际上我只需要一半 当我尝试访问前半部分的长度时,出现以下错误: 索引2超出大小为2的轴0的界限 我得到的与我需要的(红色)Python 当我只需要1个内部列表时,循环打印长度为2个内部列表,python,for-loop,Python,For Loop,我有一个xy坐标的列表,如下所示: [xcoords][ycoords]] 因为我的For循环使用整个列表的长度,所以它打印90多个结果,而实际上我只需要一半 当我尝试访问前半部分的长度时,出现以下错误: 索引2超出大小为2的轴0的界限 我得到的与我需要的(红色) 这是因为您的[positions]变量中有一个dup,请尝试打印它: import numpy as np import matplotlib.pyplot as plt import shapely.geometry from sh
这是因为您的[positions]变量中有一个dup,请尝试打印它:
import numpy as np
import matplotlib.pyplot as plt
import shapely.geometry
from shapely.geometry import LineString
from shapely.geometry import Point, Polygon
import descartes
quality = 7
x = np.linspace(-1,1,quality)
y = np.linspace(-1,1,quality)
X,Y = np.meshgrid(x,y)
polycoords = [[-1, 1], [-1, 0.5], [0, 0.5], [0, 1]]
clip_poly = shapely.geometry.Polygon(polycoords)
positions = np.vstack([Y.ravel(), X.ravel()])
print(positions)
[[-1. -1. -1. -1. -1. -1.
-1. -0.66666667 -0.66666667 -0.66666667 -0.66666667 -0.66666667
-0.66666667 -0.66666667 -0.33333333 -0.33333333 -0.33333333 -0.33333333
-0.33333333 -0.33333333 -0.33333333 0. 0. 0.
0. 0. 0. 0. 0.33333333 0.33333333
0.33333333 0.33333333 0.33333333 0.33333333 0.33333333 0.66666667
0.66666667 0.66666667 0.66666667 0.66666667 0.66666667 0.66666667
1. 1. 1. 1. 1. 1.
1. ]
[-1. -0.66666667 -0.33333333 0. 0.33333333 0.66666667
1. -1. -0.66666667 -0.33333333 0. 0.33333333
0.66666667 1. -1. -0.66666667 -0.33333333 0.
0.33333333 0.66666667 1. -1. -0.66666667 -0.33333333
0. 0.33333333 0.66666667 1. -1. -0.66666667
-0.33333333 0. 0.33333333 0.66666667 1. -1.
-0.66666667 -0.33333333 0. 0.33333333 0.66666667 1.
-1. -0.66666667 -0.33333333 0. 0.33333333 0.66666667
1. ]]
它们不是DUP,而是对应的x和y值,尽管它们看起来可能很像。职位列表的结构为[[xcoords][ycoords]]。但因为它们是更大列表中的独立列表。循环贯穿整个长度。
import numpy as np
import matplotlib.pyplot as plt
import shapely.geometry
from shapely.geometry import LineString
from shapely.geometry import Point, Polygon
import descartes
quality = 7
x = np.linspace(-1,1,quality)
y = np.linspace(-1,1,quality)
X,Y = np.meshgrid(x,y)
polycoords = [[-1, 1], [-1, 0.5], [0, 0.5], [0, 1]]
clip_poly = shapely.geometry.Polygon(polycoords)
positions = np.vstack([Y.ravel(), X.ravel()])
print(positions)
[[-1. -1. -1. -1. -1. -1.
-1. -0.66666667 -0.66666667 -0.66666667 -0.66666667 -0.66666667
-0.66666667 -0.66666667 -0.33333333 -0.33333333 -0.33333333 -0.33333333
-0.33333333 -0.33333333 -0.33333333 0. 0. 0.
0. 0. 0. 0. 0.33333333 0.33333333
0.33333333 0.33333333 0.33333333 0.33333333 0.33333333 0.66666667
0.66666667 0.66666667 0.66666667 0.66666667 0.66666667 0.66666667
1. 1. 1. 1. 1. 1.
1. ]
[-1. -0.66666667 -0.33333333 0. 0.33333333 0.66666667
1. -1. -0.66666667 -0.33333333 0. 0.33333333
0.66666667 1. -1. -0.66666667 -0.33333333 0.
0.33333333 0.66666667 1. -1. -0.66666667 -0.33333333
0. 0.33333333 0.66666667 1. -1. -0.66666667
-0.33333333 0. 0.33333333 0.66666667 1. -1.
-0.66666667 -0.33333333 0. 0.33333333 0.66666667 1.
-1. -0.66666667 -0.33333333 0. 0.33333333 0.66666667
1. ]]