python中的enumerate获取迭代元素的下标

mac2022-06-30  27

以前迭代的时候,需要获取次数都是如下格式:

index=1 for node in nodes: if index==3: continue print(node.text_content())index+=1

  通过for循环外层定义一个变量来进行循环,然后内部进行++操作。

 

然后今天才发现还有一个迭代同时获取元素下标的属性   如下

for i ,row in enumerate(rows): print("现在是第几个数{}了".format(i))

  

转载于:https://www.cnblogs.com/c-x-a/p/8483386.html

最新回复(0)