在矢量python的某些位置迭代

时间:2017-03-16 20:33:19

标签: python vector

我在向量中迭代,但我只需要在偶数位置迭代:

c = ([coord[0], coord[1])
a = coord[0]
b = coord[1]

for x,y in zip(a,b): 
    rectObs.append(pygame.Rect((y,x),(10,10)))

for rect in rectObs:
    pygame.draw.rect(screen, black, rect,10)

此代码绘制所有位置,但我只需要偶数位置 例如:

a = coord[0] = [4(pos0), 8(pos1), 1(pos2), 10(pos3), 7(pos4), 5(pos5)]
b = coord[1] = [5(pos0), 2(pos1), 7(pos2), 6(pos3), 1(pos4), 11(pos5)]

我需要在向量"a"中仅绘制[4, 1, 7]和向量"b" [5, 7, 1]

1 个答案:

答案 0 :(得分:0)

尝试:

vec = list(zip(a,b))
for i in range(0,len(vec),2):
    x,y = vec[i]
    ...

此外:

for x,y in list(zip(a,b))[::2]:
    ...