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

    <small id='5cXlx'></small><noframes id='5cXlx'>

    1. <legend id='5cXlx'><style id='5cXlx'><dir id='5cXlx'><q id='5cXlx'></q></dir></style></legend>
      <tfoot id='5cXlx'></tfoot>
        <bdo id='5cXlx'></bdo><ul id='5cXlx'></ul>
      1. 使用 Python 迭代字符串中的每个字符

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

      3. <tfoot id='lRHvO'></tfoot>
        <legend id='lRHvO'><style id='lRHvO'><dir id='lRHvO'><q id='lRHvO'></q></dir></style></legend>

            <tbody id='lRHvO'></tbody>

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

                1. 本文介绍了使用 Python 迭代字符串中的每个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 C++ 中,我可以像这样遍历 std::string:

                  In C++, I can iterate over an std::string like this:

                  std::string str = "Hello World!";
                  
                  for (int i = 0; i < str.length(); ++i)
                  {
                      std::cout << str[i] << std::endl;
                  }
                  

                  如何在 Python 中迭代字符串?

                  How do I iterate over a string in Python?

                  推荐答案

                  正如 Johannes 指出的那样,

                  As Johannes pointed out,

                  for c in "string":
                      #do something with c
                  

                  您可以使用 for 循环 构造在 python 中迭代几乎任何东西,

                  You can iterate pretty much anything in python using the for loop construct,

                  例如,open("file.txt") 返回一个文件对象(并打开文件),遍历它会遍历该文件中的行

                  for example, open("file.txt") returns a file object (and opens the file), iterating over it iterates over lines in that file

                  with open(filename) as f:
                      for line in f:
                          # do something with line
                  

                  如果这看起来很神奇,那么它有点像,但它背后的想法真的很简单.

                  If that seems like magic, well it kinda is, but the idea behind it is really simple.

                  有一个简单的迭代器协议可以应用于任何类型的对象,以使 for 循环在其上工作.

                  There's a simple iterator protocol that can be applied to any kind of object to make the for loop work on it.

                  只需实现一个定义 next() 方法的迭代器,并在一个类上实现一个 __iter__ 方法以使其可迭代.(__iter__当然应该返回一个迭代器对象,即定义next()的对象)

                  Simply implement an iterator that defines a next() method, and implement an __iter__ method on a class to make it iterable. (the __iter__ of course, should return an iterator object, that is, an object that defines next())

                  查看官方文档

                  这篇关于使用 Python 迭代字符串中的每个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:pandas 中的 for 循环真的很糟糕吗?我什么时候应该关心? 下一篇:当在应用中也计算前一个值时,Pandas 中是否可以使用 dataframe.apply 中的前一行值?

                  相关文章

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

                2. <tfoot id='Fw81u'></tfoot>
                  1. <legend id='Fw81u'><style id='Fw81u'><dir id='Fw81u'><q id='Fw81u'></q></dir></style></legend>

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