![](/img/trans.png)
[英]How to enumerate over a list in python backwards with index(From the end to the beginning)
[英]Python enumerate list setting start index but without increasing end count
我想遍历一个列表,计数器从零开始,但列表的起始索引为 1,例如:
valueList = [1, 2, 3, 4]
secondList = ['a', 'b', 'c', 'd']
for i, item in enumerate(valueList, start=1):
print(secondList[i])
代码因索引超出范围错误而失败(我意识到这是因为我以列表的长度 -1 加上起始值结束,并且 Python 列表的索引为零,因此以这种方式使用 i 来调用另一个中的第 i 个元素列表无效)。 下面的工作,但对 i 大于零的测试的添加看起来不是 Pythonic。
valueList = [1, 2, 3, 4]
secondList = ['a', 'b', 'c', 'd']
for i, item in enumerate(valueList, start=0):
if i > 0:
print(secondList[i])
枚举不是正确的选择,还有其他方法吗?
听起来好像您想对列表进行切片; 仍然从一开始enumerate()
以获得相同的索引:
for i, item in enumerate(valueList[1:], start=1):
然后从第二个元素开始循环遍历valueList
,并使用匹配的索引:
>>> valueList = [1, 2, 3, 4]
>>> secondList = ['a', 'b', 'c', 'd']
>>> for i, item in enumerate(valueList[1:], start=1):
... print(secondList[i])
...
b
c
d
在这种情况下,我只使用zip()
代替,也许结合itertools.islice()
:
from itertools import islice
for value, second in islice(zip(valueList, secondList), 1, None):
print(value, second)
islice()
调用会为您跳过第一个元素:
>>> from itertools import islice
>>> for value, second in islice(zip(valueList, secondList), 1, None):
... print(value, second)
...
2 b
3 c
4 d
问题不是枚举,也不是start
参数,而是当您执行start=1
,您是从1
枚举到valueList+1
的事实:
>>> valueList = [1, 2, 3, 4]
>>> secondList = ['a', 'b', 'c', 'd']
>>> for i, item in enumerate(valueList, start=1):
... print(i)
... print(secondList[i])
... print('----')
...
1
b
----
2
c
----
3
d
----
4
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
IndexError: list index out of range
所以当然,当您尝试访问secondList[4]
,没有可用的值! 你可能想要这样做:
>>> for i, item in enumerate(valueList, start=1):
... if i < len(secondList):
... print(secondList[i])
...
b
c
d
也就是说,我不确定你到底想要达到什么目的。 如果您想跳过secondList
的第一个值,这可能是一个解决方案,即使不是最有效的解决方案。 更好的方法是实际使用切片运算符:
>>> print(secondList[1:])
['b', 'c', 'd']
如果您想使用自然枚举(而不是计算机的枚举)迭代列表,即从1
而不是0
,那么这不是要走的路。 要显示自然索引并使用计算机索引,您只需执行以下操作:
>>> for i, item in enumerate(valueList):
... print("{} {}".format(i+1, secondList[i]))
...
1 a
2 b
3 c
4 d
最后,您可以使用zip()
而不是 enumerate 来链接两个列表的内容:
>>> for i, item in zip(valueList, secondList):
... print('{} {}'.format(i, item))
...
1 a
2 b
3 c
4 d
这将显示valueList
每个值与相同索引处的secondList
的值。
如果您的集合是生成器,您最好这样做:
for i, item in enumerate(valueList, start=1):
if i < 1:
continue
print(secondList[i])
或者修改 Martijn Pieters 的好答案,
from itertools import islice, izip
for value, second in islice(izip(valueList, secondList), 1, None):
print(value, second)
这样数据是延迟加载的。
或者不使用 start-parameter,你可以做一个 if-comparison i > 0。这样你也不必在最后处理 list+1 索引错误:
for i, item in enumerate(valueList):
if i > 0:
continue
print(secondList[i])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.