简体   繁体   English

Python基础知识:对于我来说,枚举(seq)中的元素..为什么/如何工作?

[英]Python Basics: for i, element in enumerate(seq)..why/how does this work?

Hello I've found an intersting snippet: 您好,我发现了一个有趣的代码段:

seq = ["one", "two", "three"] #edited
for i, element in enumerate(seq):
    seq[i] = '%d: %s' % (i, seq[i])

>>> seq
['0: one', '1: two', '2: three']

I wonder how python is doing that.... for me element should be undefined...but obviously it isn't..what does python do here? 我不知道python是如何做到的。...对我来说,元素应该是未定义的...但显然不是。.python在这里做什么?

Thanks a lot! 非常感谢!

enumerate essentially turns each element of the input list into a list of tuples with the first element as the index and the element as the second. enumerate实质上将输入列表的每个元素转换为元组列表,其中第一个元素为索引,而第二个为元素。 enumerate(['one', 'two', 'three']) therefore turns into [(0, 'one'), (1, 'two'), (2, 'three')] enumerate(['one', 'two', 'three'])因此变成[(0, 'one'), (1, 'two'), (2, 'three')]

The bit just after the for pretty much assigns i to the first element and element to the second for each tuple in the enumeration. 该位刚过for几乎受让人i的第一个元素和element到第二对于列举中的每个元组。 So for example in the first iteration, i == 0 and element == 'one' , and you just go through the other tuples to get the values for the other iterations. 因此,例如在第一次迭代中, i == 0element == 'one' ,您只需遍历其他元组即可获取其他迭代的值。

The actual definition of enumerate function is like this 枚举函数的实际定义是这样的

enumerate(iterable[, start]) -> iterator for index, value of iterable

Return an enumerate object.  iterable must be another object that supports
iteration.  The enumerate object yields pairs containing a count (from
start, which defaults to zero) and a value yielded by the iterable argument.
enumerate is useful for obtaining an indexed list:
    (0, seq[0]), (1, seq[1]), (2, seq[2]), ...

But the parameter seq if not defined will not return the desired output, rather it will throw an error like- NameError: name 'seq' is not defined. 但是,如果未定义参数seq,则不会返回所需的输出,而是会引发类似NameError的错误:Name seq未定义。

i am not sure what you want, to know enumerate or what's happening here in this . 我不确定您想要什么,要了解枚举或此处发生了什么。 may this can help : 这可以帮助:

seq = ["one", "two", "three"]
for i range(3):
    seq[i] = '%d: %s' % (i, seq[i])

>>> seq
 ['0: one', '1: two', '2: three']

actually , here "seq" is object of list-class. 实际上,这里的“ seq”是列表类的对象。 Each element in list reference to independent object of different class . 列表中的每个元素都引用不同类的独立对象。 when in above function ; 在上述功能中; first you get the reference of each list element's object by accessing "seq[i]", you create a new object using referenced object value (can belongs to other class), now u again referencing the "seq[i]" to new created object for each element . 首先,通过访问“ seq [i]”获得每个列表元素对象的引用,然后使用引用的对象值创建一个新对象(可以属于其他类),然后再次引用“ seq [i]”来创建新对象每个元素的对象。

You are right. 你是对的。 seq is not defined. seq未定义。 Your snippet will not work. 您的代码段无效。 It will only work if seq has ben set to seq = ['one', 'two', 'three'] before. 只有在seq之前被设置为seq = ['one','two','three']时,它才有效。 Just try it! 去尝试一下!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM