<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' 关键字只返回循环中的一个元素?

        时间:2023-09-02
            <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' 关键字只返回循环中的一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在函数的代码对象上使用 Python exec 时如何获取返回值? 下一篇:Python SyntaxError: ("'return' with argument in

                  相关文章

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

                  1. <tfoot id='PF3zB'></tfoot>
                  2. <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>