通过while循环查找索引

时间:2017-11-06 19:36:56

标签: python python-2.7 indexing while-loop

如何通过 while循环(不是)来打印列表中的索引?

示例1:

import onedrivesdk
from onedrivesdk.helpers import GetAuthCodeServer

redirect_uri = 'http://localhost:8080/'
client_secret = 'vwIK558@{[pscjkCHCMI68&'
scopes=['wl.signin', 'wl.offline_access', 'onedrive.readwrite']
client = onedrivesdk.get_default_client(
    client_id='0000000048201716', 
    scopes=scopes)
auth_url = client.auth_provider.get_auth_url(redirect_uri)
code = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri)
client.auth_provider.authenticate(code, redirect_uri, client_secret)

在这种情况下,我希望python(2.7)打印Lst中可以由c分割的数字索引。 (LST [ 1 ],LST [ 4 ])

示例2:

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

c=2

在这种情况下,我希望python(2.7)打印''。

1 个答案:

答案 0 :(得分:1)

这正是您所寻找的:

#!/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"