Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 访问迭代器第n个元素的next(next(…)的替代方法_Python_Iterator - Fatal编程技术网

Python 访问迭代器第n个元素的next(next(…)的替代方法

Python 访问迭代器第n个元素的next(next(…)的替代方法,python,iterator,Python,Iterator,我有一个列表迭代器子迭代器相当于(VS code debugger),需要访问迭代器的第4个元素,我知道它确实存在 有没有一种快速的方法,不用调用4次next(children)来访问列表迭代器中第n个位置的元素,而不用调用列表本身 谢谢 编辑:以下是一些代码: 迭代器实际上是div virgin_url = "https://www.clicpublic.be/product/_" product_soup = get_soup(virgin_url + single_id + ".html"

我有一个列表迭代器
子迭代器
相当于
(VS code debugger),需要访问迭代器的第4个元素,我知道它确实存在

有没有一种快速的方法,不用调用4次
next(children)
来访问列表迭代器中第n个位置的元素,而不用调用列表本身

谢谢

编辑:以下是一些代码: 迭代器实际上是
div

virgin_url = "https://www.clicpublic.be/product/_"
product_soup = get_soup(virgin_url + single_id + ".html") 
#get_soup returns the BeatutifulSoup([HTML of page])
bs_info_list = product_soup.findAll("div", {'class': "txtProductInformation"}
children = bs_info_list[0].children
您可以使用:

第四个元素有索引3

例:

输出:

3

请参阅itertools文档的recipes部分中的
nth
,那么您是否有嵌套迭代器,如“next(next(…)”所示?您不能为此使用BeautifulSoup功能吗?你能分享相关的HTML源代码吗?
from itertools import islice

children = (e for e in range(100))
next(islice(children, 3, None))
3