gpt4 book ai didi

python - 我可以在不创建自己的计数器的情况下显示每个列表项在列表中的位置吗?

转载 作者:太空宇宙 更新时间:2023-11-04 08:16:39 26 4
gpt4 key购买 nike

我正在尝试使用 BeautifulSoup 解析一个表,并发现在浏览它时了解我正在查看的行和列会很有帮助。现在我得到了这个:

for table in soup.find_all("table", {"class":"foo"}):
r = 0
for row in table.find_all('tr'):
cells = row.find_all("td")
c = 0
for cell in cells:
print "row", r, "cell", c
print cell.attr
c += 1
r +=1

这转储了一些揭示信息:

row 0 cell 0 
row 1 cell 0
row 1 cell 1
row 1 cell 2
row 1 cell 3
row 1 cell 4
row 2 cell 0
row 2 cell 1
row 3 cell 0
row 3 cell 1

由于某种原因,行 [1] 有很多额外的列。方便知道。我想知道的是......是否有一个内置变量可以报告我在列表中的位置。

最佳答案

你在寻找enumerate吗?

for c,cell in enumerate(cells):
....

关于python - 我可以在不创建自己的计数器的情况下显示每个列表项在列表中的位置吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13371891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com