<i id='tPqs9'><tr id='tPqs9'><dt id='tPqs9'><q id='tPqs9'><span id='tPqs9'><b id='tPqs9'><form id='tPqs9'><ins id='tPqs9'></ins><ul id='tPqs9'></ul><sub id='tPqs9'></sub></form><legend id='tPqs9'></legend><bdo id='tPqs9'><pre id='tPqs9'><center id='tPqs9'></center></pre></bdo></b><th id='tPqs9'></th></span></q></dt></tr></i><div id='tPqs9'><tfoot id='tPqs9'></tfoot><dl id='tPqs9'><fieldset id='tPqs9'></fieldset></dl></div>

    <small id='tPqs9'></small><noframes id='tPqs9'>

    <legend id='tPqs9'><style id='tPqs9'><dir id='tPqs9'><q id='tPqs9'></q></dir></style></legend>

      • <bdo id='tPqs9'></bdo><ul id='tPqs9'></ul>
    1. <tfoot id='tPqs9'></tfoot>

      1. 范围(len(list))还是枚举(list)?

        时间:2023-10-19
        1. <i id='KPjgZ'><tr id='KPjgZ'><dt id='KPjgZ'><q id='KPjgZ'><span id='KPjgZ'><b id='KPjgZ'><form id='KPjgZ'><ins id='KPjgZ'></ins><ul id='KPjgZ'></ul><sub id='KPjgZ'></sub></form><legend id='KPjgZ'></legend><bdo id='KPjgZ'><pre id='KPjgZ'><center id='KPjgZ'></center></pre></bdo></b><th id='KPjgZ'></th></span></q></dt></tr></i><div id='KPjgZ'><tfoot id='KPjgZ'></tfoot><dl id='KPjgZ'><fieldset id='KPjgZ'></fieldset></dl></div>
            <tbody id='KPjgZ'></tbody>

            <bdo id='KPjgZ'></bdo><ul id='KPjgZ'></ul>

            <small id='KPjgZ'></small><noframes id='KPjgZ'>

                <legend id='KPjgZ'><style id='KPjgZ'><dir id='KPjgZ'><q id='KPjgZ'></q></dir></style></legend>
                  <tfoot id='KPjgZ'></tfoot>
                  本文介绍了范围(len(list))还是枚举(list)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  可能重复:
                  只需要索引:枚举或(x)范围?

                  哪些会被认为更好/更清晰/更快/更Pythonic"?我不关心列表L的内容,只关心它有多长.

                  Which of these would be considered better/clearer/faster/more 'Pythonic'? I don't care about the content of the list L, just how long it is.

                  a = [f(n) for n, _ in enumerate(L)]
                  

                  a = [f(n) for n in range(len(L))]
                  

                  如果有什么不同,f 函数也会使用 len(list).

                  If it makes any difference, the function f makes use of len(list) as well.

                  推荐答案

                  一些快速的计时运行似乎给使用 range() 的第二个选项比 enumerate():

                  Some quick timing runs seem to give the 2nd option using range() a slight edge over enumerate():

                  timeit a = [f(n) for n, _ in enumerate(mlist)]
                  10000 loops, best of 3: 118 us per loop
                  
                  timeit a = [f(n) for n in range(len(mlist))]
                  10000 loops, best of 3: 102 us per loop
                  

                  只是为了好玩,使用 xrange() (Python v2.7.2)

                  and just for fun using xrange() (Python v2.7.2)

                  timeit a = [f(n) for n in xrange(len(mlist))]
                  10000 loops, best of 3: 99 us per loop
                  

                  我倾向于首先使用可读代码,然后使用 xrange()(如果可用)(即 Pre-Python v 3.x),然后是 range()enumerate().

                  I would favor readable code first, then using xrange() if available (i.e., Pre-Python v 3.x), followed by range() and enumerate().

                  这篇关于范围(len(list))还是枚举(list)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <tfoot id='ldOBI'></tfoot>

                • <i id='ldOBI'><tr id='ldOBI'><dt id='ldOBI'><q id='ldOBI'><span id='ldOBI'><b id='ldOBI'><form id='ldOBI'><ins id='ldOBI'></ins><ul id='ldOBI'></ul><sub id='ldOBI'></sub></form><legend id='ldOBI'></legend><bdo id='ldOBI'><pre id='ldOBI'><center id='ldOBI'></center></pre></bdo></b><th id='ldOBI'></th></span></q></dt></tr></i><div id='ldOBI'><tfoot id='ldOBI'></tfoot><dl id='ldOBI'><fieldset id='ldOBI'></fieldset></dl></div>

                  <small id='ldOBI'></small><noframes id='ldOBI'>

                • <legend id='ldOBI'><style id='ldOBI'><dir id='ldOBI'><q id='ldOBI'></q></dir></style></legend>

                          <tbody id='ldOBI'></tbody>
                        • <bdo id='ldOBI'></bdo><ul id='ldOBI'></ul>