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

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

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

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

        在for循环中不更改列表项

        List items are not changed in for loop(在for循环中不更改列表项)

          <tbody id='S3cUZ'></tbody>

        1. <tfoot id='S3cUZ'></tfoot>
              <bdo id='S3cUZ'></bdo><ul id='S3cUZ'></ul>

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

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

                <legend id='S3cUZ'><style id='S3cUZ'><dir id='S3cUZ'><q id='S3cUZ'></q></dir></style></legend>
                1. 本文介绍了在for循环中不更改列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  当以下代码没有执行我预期的操作时,我感到震惊:

                  lines_list = ['this is line 1
                  ', 'this is line 2
                  ', 'this is line 3
                  ']
                  for line in lines_list:
                      line = line.strip()
                  

                  PerlJava中,我的用法运行良好。Python是具有独特行为的那个。

                  在Java中,这将起作用:

                  String[] lines = {"this is line 1
                  ", "this is line 2
                  ", "this is line 3
                  "};
                  for (String line : lines) {
                      line = line.trim();
                  }
                  

                  在Perl中,这将起作用:

                  my @lines = ("this is line 1
                  ", "this is line 2
                  ", "this is line 3
                  ");
                  foreach my $line (@lines) {
                      $line =~ s/s+$//;
                      $line =~ s/^s+//;
                  }
                  

                  我当然希望列表中的每一项都变成"stripped",即在这种情况下,没有尾随的' '字符,但它没有.

                  print lines_list
                  

                  输出:

                  ['this is line 1
                  ', 'this is line 2
                  ', 'this is line 3
                  ']
                  

                  有没有在for循环期间更改列表项的优雅方法?我不想复制该列表.

                  推荐答案

                  您可以按索引浏览,并以此方式就地修改

                  for i, _ in enumerate(lines_list):
                      lines_list[i] = lines_list[i].strip()
                  

                  虽然我想很多人更喜欢简单地复制列表,如果列表不太大而导致问题的话

                  lines_list = [line.strip() for line in lines_list]
                  
                  问题是使用=运算符重新赋值给变量line,它不会影响原始字符串的内容。新的Python程序员同样经常在以下情况下感到惊讶:

                  for i in range(10):
                      print(i)
                      i += 1
                  
                  打印数字0、1、2、3、4、5、6、7、8、9。这是因为for循环在每次迭代开始时将i重新分配给范围中的下一个元素。这不完全是你的问题,但很相似。

                  由于您正在读取文件中的行,然后将其剥离,因此您真正应该做的是

                  with open(file_name) as f:
                      lines_list = [line.strip() for line in f]
                  

                  一次读取并剥离一行,而不是先读取所有内容,然后再剥离行

                  这篇关于在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;?(为什么我的函数输出打印出“无?)
                  <i id='yVCK7'><tr id='yVCK7'><dt id='yVCK7'><q id='yVCK7'><span id='yVCK7'><b id='yVCK7'><form id='yVCK7'><ins id='yVCK7'></ins><ul id='yVCK7'></ul><sub id='yVCK7'></sub></form><legend id='yVCK7'></legend><bdo id='yVCK7'><pre id='yVCK7'><center id='yVCK7'></center></pre></bdo></b><th id='yVCK7'></th></span></q></dt></tr></i><div id='yVCK7'><tfoot id='yVCK7'></tfoot><dl id='yVCK7'><fieldset id='yVCK7'></fieldset></dl></div>

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

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

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

                          <tbody id='yVCK7'></tbody>

                          • <bdo id='yVCK7'></bdo><ul id='yVCK7'></ul>