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

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

      <legend id='HFdfD'><style id='HFdfD'><dir id='HFdfD'><q id='HFdfD'></q></dir></style></legend>
      • <bdo id='HFdfD'></bdo><ul id='HFdfD'></ul>

    2. 我的 For 循环不会遍历列表

      My For loop won#39;t iterate through a list(我的 For 循环不会遍历列表)
      <legend id='OHRyR'><style id='OHRyR'><dir id='OHRyR'><q id='OHRyR'></q></dir></style></legend>
        <tbody id='OHRyR'></tbody>
        • <bdo id='OHRyR'></bdo><ul id='OHRyR'></ul>
        • <tfoot id='OHRyR'></tfoot>

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

                本文介绍了我的 For 循环不会遍历列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我必须确定列表中的所有数字是否都是素数,然后根据结果返回布尔真"或假"语句.我在 for 循环中做了一些条件语句来查看数字是否为素数.

                I have to determine if all the numbers in a list are prime numbers and then return a boolean "True" or "False" statement depending on the outcome. I made some conditional statements inside of a for loop to see if the number was prime or not.

                代码如下:

                def all_primes(xs):
                    is_prime = None
                    for i in xs:
                        if i < 2:
                            is_prime = False
                            return is_prime
                            break
                        elif (i % 2 == 0) and (i % i == 1):
                            is_prime = False
                            return is_prime
                            break
                        else:
                            is_prime = True
                            return is_prime
                

                问题是,我在 Python Visualizer 中看到了这一点,for 循环在检查列表中的第一个值后停止迭代.我不明白为什么语法与我过去使用的 for 循环相同.

                The problem is, and I saw this in the Python Visualizer, the for loop stops iterating after checking the first value in the list. I don't understand why as the syntax is the same as for loops I've used in the past.

                我插入了一些示例值,例如:all_primes([5,2,11,37])all_primes([5,2,4,37])并且返回值始终为真,因为 5 是列表中的第一个数字,也是唯一被迭代的数字.

                I plugged in some example values like: all_primes([5,2,11,37]) or all_primes([5,2,4,37]) and the return value is always true since 5 is the first number on the list and the only number that is being iterated.

                关于为什么的任何想法?

                Any ideas as to why?

                推荐答案

                你的 if/else 中有一个 return 和一个 break块,你应该摆脱它们.else 中的 return 也应该在外面,否则只要他找到素数",它就会返回.

                You have a return and a break in your if/else block, you should get rid of them. Also the return in the else should be outside, or it will just return whenever he finds a "prime".

                def all_primes(xs):
                  is_prime = None
                  for i in xs:
                      if i < 2:
                          is_prime = False
                          return is_prime
                      elif (i % 2 == 0):
                          is_prime = False
                          return is_prime
                      else:
                          is_prime = True
                  return is_prime
                

                在此之后,您应该知道,您并没有真正检查素数.这不是最有效的方法,但它很清楚如何:

                After this, you should know, that you are not really checking primes. Here is not the most efficient way but its clear how to:

                def all_primes(xs):
                    def checkPrime(n):
                        if n < 2:
                            return False
                        for i in xrange(2, n):
                            if n%i == 0:
                                return False
                        return True
                    return all(map(checkPrime, xs))
                

                如果没有 map 函数,您只需要使用 for 循环进行迭代:

                without the map functions, you just have to iterate with a for loop:

                def all_primes(xs):
                    def checkPrime(n):
                        if n < 2:
                            return False
                        for i in xrange(2, n):
                            if n%i == 0:
                                return False
                        return True
                    for n in xs:
                        if not checkPrime(n):
                            return False
                    return True
                

                这篇关于我的 For 循环不会遍历列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                <legend id='bsrTW'><style id='bsrTW'><dir id='bsrTW'><q id='bsrTW'></q></dir></style></legend>
                  <tbody id='bsrTW'></tbody>
                  • <i id='bsrTW'><tr id='bsrTW'><dt id='bsrTW'><q id='bsrTW'><span id='bsrTW'><b id='bsrTW'><form id='bsrTW'><ins id='bsrTW'></ins><ul id='bsrTW'></ul><sub id='bsrTW'></sub></form><legend id='bsrTW'></legend><bdo id='bsrTW'><pre id='bsrTW'><center id='bsrTW'></center></pre></bdo></b><th id='bsrTW'></th></span></q></dt></tr></i><div id='bsrTW'><tfoot id='bsrTW'></tfoot><dl id='bsrTW'><fieldset id='bsrTW'></fieldset></dl></div>

                      • <tfoot id='bsrTW'></tfoot>
                          <bdo id='bsrTW'></bdo><ul id='bsrTW'></ul>

                        • <small id='bsrTW'></small><noframes id='bsrTW'>