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

    1. <small id='QLypO'></small><noframes id='QLypO'>

        <bdo id='QLypO'></bdo><ul id='QLypO'></ul>
      <legend id='QLypO'><style id='QLypO'><dir id='QLypO'><q id='QLypO'></q></dir></style></legend>

      1. 在“for"循环中访问索引?

        时间:2024-04-21

          • <small id='wDSbN'></small><noframes id='wDSbN'>

            <legend id='wDSbN'><style id='wDSbN'><dir id='wDSbN'><q id='wDSbN'></q></dir></style></legend>
            • <bdo id='wDSbN'></bdo><ul id='wDSbN'></ul>

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

                  本文介绍了在“for"循环中访问索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  How do I access the index in a for loop like the following?

                  ints = [8, 23, 45, 12, 78]
                  for i in ints:
                      print('item #{} = {}'.format(???, i))
                  

                  I want to get this output:

                  item #1 = 8
                  item #2 = 23
                  item #3 = 45
                  item #4 = 12
                  item #5 = 78
                  

                  When I loop through it using a for loop, how do I access the loop index, from 1 to 5 in this case?

                  解决方案

                  Using an additional state variable, such as an index variable (which you would normally use in languages such as C or PHP), is considered non-pythonic.

                  The better option is to use the built-in function enumerate(), available in both Python 2 and 3:

                  for idx, val in enumerate(ints):
                      print(idx, val)
                  

                  Check out PEP 279 for more.

                  这篇关于在“for"循环中访问索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:“u"到底是什么意思?和“r"字符串标志,什么是原始字符串文字? 下一篇:什么是真假?它与真假有什么不同?

                  相关文章

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

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

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

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

                    1. <tfoot id='Al2S0'></tfoot>