遍历列表中的偶数索引

时间:2018-08-02 17:47:22

标签: python python-3.x

说我有此列表:

CP = [1,0,1,0]

如何通过索引即CP [i]在列表中仅打印0。

必填输出:

0
0

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

这感觉像是一个作业问题,我假设您实际上是想要奇数索引而不是偶数索引。

这是一个可能的解决方案:

# get a range of all odd numbers between 1 and length of list
# with a step size of 2
for i in range(1, len(CP), 2):
    print(CP[i])

答案 1 :(得分:0)

一个班轮-

print ('\n'.join([str(CP[i]) for i in range(1, len(CP), 2)]))