【问题标题】:What is enumerate in Python mean? [duplicate]Python中的枚举是什么意思? [复制]
【发布时间】:2016-09-29 17:43:06
【问题描述】:

<enumerate object at 0x000000000302E2D0> 是什么意思?

>>> my_list = ['apple', 'banana', 'grapes', 'pear']
>>> enumerate(my_list)
<enumerate object at 0x000000000302E2D0>

我尝试了 Google,但仍然不明白为什么我们有 <enumerate object at 0x000000000302E2D0>。你能帮我解决这个问题吗?
谢谢。

【问题讨论】:

标签: python enumerated-types


【解决方案1】:

它返回一个枚举对象,它是一个迭代器。在您明确要求它之前,它实际上不会向您显示它包含的内容。一种方法是强制它成为一个列表。

>>> my_list = ['apple', 'banana', 'grapes', 'pear']
>>> a = enumerate(my_list)
>>> a
<enumerate at 0x7ffff27d0630>
>>> list(a)
[(0, 'apple'), (1, 'banana'), (2, 'grapes'), (3, 'pear')]

您还可以在 for 循环中迭代枚举对象。

查看this question 了解有关迭代器的更多信息。

【讨论】:

    【解决方案2】:

    来自enumerate.__doc__

    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]), ...
    

    【讨论】:

      猜你喜欢
      • 2013-07-27
      • 2017-01-27
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      • 2015-02-13
      • 2019-03-30
      • 1970-01-01
      • 2010-09-05
      相关资源
      最近更新 更多