[英]How to write a enumerate loop
I was wondering if it is possible to run an enumerate loop as as we can see below我想知道是否可以运行枚举循环,如下所示
int_term = [scipy.quad(t_x,x0,i)[0] for i in enumerate(x_vals)]
or do we have to write it this way还是我们必须这样写
for i in enumerate(x_vals)
Hopefully this clarifies what exactly enumerate does:希望这可以澄清 enumerate 的确切作用:
>>> list(enumerate('abc'))
[(0, 'a'), (1, 'b'), (2, 'c')]
It just turns your list into a list of a pairs where each of your elements is joined with the index ie a
with 0, b
with 1 and c
with 2.它只是将您的列表变成一对列表,其中每个元素都与索引连接,即
a
与 0, b
与 1 和c
与 2。
That's not exactly true because it is an iterator rather than an actual list - so it provides you with values lazily when you wrap another iterator.这并不完全正确,因为它是一个迭代器而不是一个实际的列表——所以当你包装另一个迭代器时它会懒惰地为你提供值。
Looping over it is therefore the same as any other lists except usually it is done the following way:因此,循环它与任何其他列表相同,但通常以以下方式完成:
for i, x in enumerate(xs)
You could still use:你仍然可以使用:
for i in enumerate(xs)
but then just know that i is going to be a tuple with some int, and some object from xs
.但随后只知道 i 将成为一个带有一些 int 的元组,以及一些来自
xs
的 object 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.