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

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

        'Return' 关键字只返回循环中的一个元素?

        #39;Return#39; keyword returns only one element from a loop?(Return 关键字只返回循环中的一个元素?)
            <bdo id='tcyos'></bdo><ul id='tcyos'></ul>

            1. <legend id='tcyos'><style id='tcyos'><dir id='tcyos'><q id='tcyos'></q></dir></style></legend>
                <tbody id='tcyos'></tbody>

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

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

                2. <i id='tcyos'><tr id='tcyos'><dt id='tcyos'><q id='tcyos'><span id='tcyos'><b id='tcyos'><form id='tcyos'><ins id='tcyos'></ins><ul id='tcyos'></ul><sub id='tcyos'></sub></form><legend id='tcyos'></legend><bdo id='tcyos'><pre id='tcyos'><center id='tcyos'></center></pre></bdo></b><th id='tcyos'></th></span></q></dt></tr></i><div id='tcyos'><tfoot id='tcyos'></tfoot><dl id='tcyos'><fieldset id='tcyos'></fieldset></dl></div>
                  本文介绍了'Return' 关键字只返回循环中的一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个简单的函数来读取 csv 文件并从中提取第一个列:

                  I have a simple function to read the csv file and extracts the first coloum from it:

                  import csv 
                  
                  def pass_username():
                      with open('test.csv', 'r') as csvfile:
                          spamreader = csv.reader(csvfile, delimiter=',')
                          for row in spamreader:
                              return row[0]
                  

                  当我把这个函数称为:

                  a = pass_username()
                  print a 
                  

                  这只会打印第一个元素.但是,当我将 return 单词替换为 print 作为 print row[0] 并将函数调用为 pass_username() 它打印所有元素.我想将该函数分配给一个变量,因此我想使用 return.如何解决?

                  This only prints the first element. However, when I replace return word with print as print row[0] and call the function as pass_username() it prints all the elements. I want to assign that function to a variable thus I want to use return. How to fix it?

                  test.csv 的内容:

                  Content of test.csv:

                  "test@gmail.com","rockon"
                  "hello@gmail.com","hey"
                  "hithere@gmail.com","ok"
                  "hellosir@gmail.com","password"
                  

                  推荐答案

                  正如其他回答的人所说,您可以将结果累积到一个列表中并返回.但是,另一种方法是将 return 替换为 yield ,这会导致您的函数返回一个可迭代类型对象,该对象会在您决定稍后对其进行迭代时生成您产生的项目(可能带有 for 循环).

                  As the other people who answered said, you can accumulate the results into a list and return that. Another way though, would be to replace return with yield which causes your function to return an iterable type object that produces the items you yield when you decide to iterate over it later (possibly with a for loop).

                  请参阅:yield"是什么意思?Python中的关键字do?

                  以下是您将如何在代码中使用它:

                  Here is how you would use it with your code:

                  import csv 
                  
                  def pass_username():
                      with open('test.csv', 'r') as csvfile:
                          spamreader = csv.reader(csvfile, delimiter=',')
                          for row in spamreader:
                              yield row[0]
                  
                  username_generator = pass_username()
                  
                  # iterate through the usernames with a for loop
                  for name in username_generator:
                      print name
                  # call the list constructor which causes it to produce all of the names
                  print list(pass_username())
                  

                  请记住,用户名是根据需要生成的,因此您可以,例如,执行 username_generator.next() 这将生成下一个用户名,而无需必须全部生产.

                  Keep in mind that the usernames are produced as they are needed, so you can, for example, do username_generator.next() which will produce the next username without having to produce all of them.

                  这篇关于'Return' 关键字只返回循环中的一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

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

                    <tbody id='PF3zB'></tbody>

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