如何访问索引本身以获取如下列表?
ints = [8, 23, 45, 12, 78]
for i in ints:
print('item #{} = {}'.format(???, i))
Run Code Online (Sandbox Code Playgroud)
当我使用循环遍历它时for,如何访问循环索引,在这种情况下从1到5?
这是很常见的,我遍历一个Python列表,让双方的内容和他们的索引.我通常做的是以下内容:
S = [1,30,20,30,2] # My list
for s, i in zip(S, range(len(S))):
# Do stuff with the content s and the index i
Run Code Online (Sandbox Code Playgroud)
我发现这个语法有点难看,尤其是zip函数内部.还有更优雅/ Pythonic的方法吗?