Python 通过while循环查找索引

Python 通过while循环查找索引,python,python-2.7,indexing,while-loop,Python,Python 2.7,Indexing,While Loop,如何通过while循环打印列表中的索引 例1: 在本例中,我希望python2.7打印Lst中的数字索引,该索引可以被c分隔。lst[1],lst[4] 例2: 在本例中,我希望python2.7打印“无”。这正是您想要的: #!/usr/bin/env python Lst = [1,2,3,4,5] c=2 i = 0 res = [] while i in range(len(Lst)): if Lst[i] % 2 == 0: res.

如何通过while循环打印列表中的索引

例1:

在本例中,我希望python2.7打印Lst中的数字索引,该索引可以被c分隔。lst[1],lst[4]

例2:


在本例中,我希望python2.7打印“无”。

这正是您想要的:

#!/usr/bin/env python

Lst = [1,2,3,4,5]

c=2
i = 0
res = []
while i in range(len(Lst)):
        if Lst[i] % 2 == 0:
                res.append(Lst[i])
        i+=1
if len(res) > 0:
        print res
else:
        print "None"

我回答了,但忽略了while循环部分,它使你的问题变成了一个问题,为我的家庭作业问题编写了一个愚蠢的代码。你自己做吧。注意:对于这种情况,while循环是最糟糕的控制结构。
Lst=[1,3,3,3,5]

c=2
#!/usr/bin/env python

Lst = [1,2,3,4,5]

c=2
i = 0
res = []
while i in range(len(Lst)):
        if Lst[i] % 2 == 0:
                res.append(Lst[i])
        i+=1
if len(res) > 0:
        print res
else:
        print "None"