我需要类似下面的代码,除了需要维护原始列表的索引.因此,例如,如果i为5且数组具有10个元素,则j将等于5,6,7,8,9.下面的代码不是这样做的.
for j, compare in enumerate(array[i:]):
#do stuff
Run Code Online (Sandbox Code Playgroud)
现在我可以用更多的C/Java方式来做这件事,但有更多的pythonic方式吗?
我最初拥有的很好,但它也可以通过枚举来完成.@ cricket-007建议如果只需要索引去原始版本,如果还需要该值,则进行枚举.
原版的:
for j in range(i, len(array)):
# do stuff
Run Code Online (Sandbox Code Playgroud)
枚举:
for j, compare in enumerate(array[i:], i):
#do stuff
Run Code Online (Sandbox Code Playgroud) 我有以下类,目前正在通过运行一个调用该文件中的方法的文件来测试它。然后,我使用打印语句的混合并检查博客以确保代码有效。
我真的很想为此编写一些 pytest 单元测试并将其全部自动化,但我该怎么做呢?此外,如果身份验证不存在或已无效,它会打开浏览器并提示用户输入身份验证代码。稍后这将由 GUI 表单处理。Pytest 不接受用户输入,这是正确的;它不会是自动化的。
class BloggerInterface(object):
"""Connects to blogger api and authorises client."""
def get_credentials(self):
"""Gets google api credentials, or generates new credentials
if they don't exist or are invalid."""
scope = 'https://www.googleapis.com/auth/blogger'
flow = oauth2client.client.flow_from_clientsecrets(
'client_secret.json', scope,
redirect_uri='urn:ietf:wg:oauth:2.0:oob')
storage = oauth2client.file.Storage('credentials.dat')
credentials = storage.get()
if not credentials or credentials.invalid:
auth_uri = flow.step1_get_authorize_url()
webbrowser.open(auth_uri)
auth_code = input('Enter the auth code: ')
credentials = flow.step2_exchange(auth_code)
storage.put(credentials)
return credentials
def get_service(self):
"""Returns an authorised blogger api service."""
credentials = …Run Code Online (Sandbox Code Playgroud)