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

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

    3. “回归"在函数中只返回一个值

      quot;Returnquot; in Function only Returning one Value(“回归在函数中只返回一个值)

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

        1. <tfoot id='JTDAz'></tfoot>
        2. <legend id='JTDAz'><style id='JTDAz'><dir id='JTDAz'><q id='JTDAz'></q></dir></style></legend>
          • <small id='JTDAz'></small><noframes id='JTDAz'>

              <tbody id='JTDAz'></tbody>
              • <bdo id='JTDAz'></bdo><ul id='JTDAz'></ul>
                本文介绍了“回归"在函数中只返回一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                假设我编写了一个 for 循环,它将输出所有数字 1 到 x:

                Let's say I write a for loop that will output all the numbers 1 to x:

                x=4
                for number in xrange(1,x+1):
                    print number,
                #Output:
                1
                2
                3
                4
                

                现在,将相同的 for 循环放入函数中:

                Now, putting that same for loop into a function:

                def counter(x):
                    for number in xrange(1,x+1):
                        return number
                print counter(4)
                #Output:
                1
                

                为什么我把for循环放到一个函数中只能得到一个值?

                Why do I only obtain one value when I put the for-loop into a function?

                我一直在通过将 for 循环的所有结果附加到一个列表,然后返回该列表来回避这个问题.

                I have been evading this problem by appending all the results of the for-loop to a list, and then returning the list.

                为什么 for 循环会追加所有结果,而不只是一个?:

                Why does the for loop append all the results, and not just one?:

                def counter(x):
                    output=[]
                    for number in xrange(1,x+1):
                        output.append(number)
                    return output
                

                返回所有值的最佳方法是什么,附加到列表似乎效率很低.

                What is the best method of returning all the values, appending to a list seems very inefficient.

                推荐答案

                return 与关键字名称所暗示的完全一样.当您点击该语句时,它 返回 并且函数的其余部分不会执行.

                return does exactly like the keyword's name implies. When you hit that statement, it returns and the rest of the function is not executed.

                您可能想要的是 yield 关键字.这将创建一个生成器函数(一个返回生成器的函数).生成器是可迭代的.每次执行 yield 表达式时,它们都会生成"一个元素.

                What you might want instead is the yield keyword. This will create a generator function (a function that returns a generator). Generators are iterable. They "yield" one element each time the yield expression is executed.

                def func():
                    for x in range(10):
                        yield x
                
                generator = func()
                for item in generator:
                    print item
                

                这篇关于“回归"在函数中只返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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 与否?)
                <tfoot id='MKcHT'></tfoot>
              • <small id='MKcHT'></small><noframes id='MKcHT'>

                    <tbody id='MKcHT'></tbody>

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

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