<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"循环中访问索引?

        Accessing the index in #39;for#39; loops?(在“for循环中访问索引?)

          • <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"循环中访问索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                    <bdo id='Al2S0'></bdo><ul id='Al2S0'></ul>

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

                            <tbody id='Al2S0'></tbody>
                            <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>

                          • <tfoot id='Al2S0'></tfoot>