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

    <legend id='EXlHC'><style id='EXlHC'><dir id='EXlHC'><q id='EXlHC'></q></dir></style></legend>
  1. <small id='EXlHC'></small><noframes id='EXlHC'>

    <tfoot id='EXlHC'></tfoot>

      <i id='EXlHC'><tr id='EXlHC'><dt id='EXlHC'><q id='EXlHC'><span id='EXlHC'><b id='EXlHC'><form id='EXlHC'><ins id='EXlHC'></ins><ul id='EXlHC'></ul><sub id='EXlHC'></sub></form><legend id='EXlHC'></legend><bdo id='EXlHC'><pre id='EXlHC'><center id='EXlHC'></center></pre></bdo></b><th id='EXlHC'></th></span></q></dt></tr></i><div id='EXlHC'><tfoot id='EXlHC'></tfoot><dl id='EXlHC'><fieldset id='EXlHC'></fieldset></dl></div>
    1. 在循环中迭代字符串中的每个第 n 个元素 - python

      时间:2023-10-19
      <legend id='9m2oA'><style id='9m2oA'><dir id='9m2oA'><q id='9m2oA'></q></dir></style></legend>
          <bdo id='9m2oA'></bdo><ul id='9m2oA'></ul>
        • <i id='9m2oA'><tr id='9m2oA'><dt id='9m2oA'><q id='9m2oA'><span id='9m2oA'><b id='9m2oA'><form id='9m2oA'><ins id='9m2oA'></ins><ul id='9m2oA'></ul><sub id='9m2oA'></sub></form><legend id='9m2oA'></legend><bdo id='9m2oA'><pre id='9m2oA'><center id='9m2oA'></center></pre></bdo></b><th id='9m2oA'></th></span></q></dt></tr></i><div id='9m2oA'><tfoot id='9m2oA'></tfoot><dl id='9m2oA'><fieldset id='9m2oA'></fieldset></dl></div>

          <small id='9m2oA'></small><noframes id='9m2oA'>

            <tbody id='9m2oA'></tbody>

              • <tfoot id='9m2oA'></tfoot>

                本文介绍了在循环中迭代字符串中的每个第 n 个元素 - python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                如何遍历字符串中的每个第二个元素?

                一种方法是(如果我想遍历第 n 个元素):

                One way to do this would be(If I want to iterate over nth element):

                sample = "This is a string"
                n = 3 # I want to iterate over every third element
                i = 1
                for x in sample:
                    if i % n == 0:
                        # do something with x
                    else:
                        # do something else with x
                    i += 1
                

                是否有任何pythonic"方式来做到这一点?(我很确定我的方法不好)

                Is thery any "pythonic" way to do this? (I am pretty sure my method is not good)

                推荐答案

                如果你想每隔 n 步做一些事情,其他情况下做其他事情,你可以使用 enumerate 来获取索引,并使用模数:

                If you want to do something every nth step, and something else for other cases, you could use enumerate to get the index, and use modulus:

                sample = "This is a string"
                n = 3 # I want to iterate over every third element
                for i,x in enumerate(sample):
                    if i % n == 0:
                        print("do something with x "+x)
                    else:
                        print("do something else with x "+x)
                

                注意它不是从 1 而是从 0 开始的.如果你想要别的东西,可以给 i 添加一个偏移量.

                Note that it doesn't start at 1 but 0. Add an offset to i if you want something else.

                要仅对每个第 n 个元素进行迭代,最好的方法是使用 itertools.islice 来避免创建硬"字符串来仅对其进行迭代:

                To iterate on every nth element only, the best way is to use itertools.islice to avoid creating a "hard" string just to iterate on it:

                import itertools
                for s in itertools.islice(sample,None,None,n):
                    print(s)
                

                结果:

                T
                s
                s
                
                r
                g
                

                这篇关于在循环中迭代字符串中的每个第 n 个元素 - python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Python + MongoDB - 光标迭代太慢 下一篇:itertools.islice 与列表切片相比

                相关文章

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

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

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