【问题标题】:Making python code more efficient让python代码更高效
【发布时间】:2016-08-17 00:51:21
【问题描述】:

我有密码

num = 1
num2 = 1
num3 = 1
list = []
list2 = []
list3 = []

def numCheck1 (num):
    while num<1001:
        if (num%3==0):
            if (num%5==0):
                print num
                list.append(num)
                num+=1
                numCheck1(num)
                break
            else:
                print "error 1"
        else:
            print "error 2"
        num+=1

numCheck1(num)
total=sum(list)
print list
print total

def numCheck2 (num2):
    while num2<1001:
        if (num2%5==0):
            print num2
            list2.append(num2)
            num2+=1
            numCheck1(num2)
            break
        else:
            print "error"

numCheck2(num2)

def numCheck3 (num3):
    while num3<1001:
        if (num3%3==0):
            print num3
            list3.append(num3)
            num3+=1
            numCheck1(num3)
            break
        else:
            print "error"

numCheck3(num3)

total2 = sum(list2)
total3 = sum(list3)
overall = (total2 + total3) - total
print list2
print list3
print total2
print total3
print overall

作为我的代码的基本摘要,我有 3 个函数,以及每个函数对应的列表和变量。第一个函数检查小于等于 1000 的 3 和 5 的所有倍数。第二个函数检查小于等于 1000 的所有 5 的倍数。第三个函数检查小于等于 1000 的 3 的所有倍数。是倍数的数字被添加到相应的列表中,而相应的变量递增以允许函数检查所有数字。最后,程序计算 4 个总数:每个列表的总数,以及一个特殊总数,它将后两个总数相加并减去第一个总数,以防止过度计数。这只是程序的总体结构。

这个程序应该解决this problem(不是作业,只是好玩)。代码正在工作(据我所知;第一个函数肯定有效)但它不断使编译器崩溃(我正在使用在线编译器,repl。我想知道是否有任何方法可以使这段代码更高效。

谢谢!

【问题讨论】:

  • 我认为你达到了最大堆栈递归深度,为什么你使用递归函数? while 足以满足此目的
  • 一目了然:您似乎是从内部呼叫numCheck1。另外,不要将变量分配给名称list(或str,或其他任何保留的名称)。最后,这里有很多违反 Python 风格的元素,例如空格和括号的使用 - 查看 PEP8 了解更多信息。
  • 永远不要使用list作为变量名。
  • @Alexander,为什么不呢?
  • list 是python中的保留字,例如列表((1, 2, 3))

标签: python performance


【解决方案1】:

我已经重写了您的代码,内嵌了 cmets。您可以在此基础上进行更多改进,但我认为看看您可以在哪些方面改进此代码会对您有所帮助。

# If you use this, it should be the first line
# in your code. If you're using Python2, you *should*
# use this, because it will make switching to Python3
# that much easier.
from __future__ import print_function

# No need for these global variables
#num = 1
#num2 = 1
#num3 = 1
#list = []
#list2 = []
#list3 = []
#

# Functions should be snake_cased, not camelCase
# also, the blank space between the function name
# and the paren is inconsistent with typical
# Python code.
def num_check_one(start):
    # We'll put the return list in here. Also
    # calling it `list = []` would have shadowed
    # a builtin function. Let's give it a better name:
    multiples = []

    # If you're iterating over known values in Python
    # use a for loop over a range instead.
    for num in range(start, 1001):

        # No need to nest your ifs.
        # Parenthesis are usually just extra noise,
        # But you might find it a little clearer with
        # them to clarify the grouping
        #if (num % 3 == 0) and (num % 5 == 0):
        # This could also be written as
        # if (not num % 3) and (not num % 5):
        # Or, using De Morgan's law:
        # if not (num % 3 or num % 5):
        if num % 3 == 0 and num % 5 == 0:
            print(num)
            multiples.append(num)
            # Uh, no need to recursively call
            # your function here.
            #numCheck1(num)
            #break
        # And finally, you don't need any of these
#            else:
#                print "error 1"
#        else:
#            print "error 2"
#        num+=1
    return multiples


# You can use the keyword in your function call
# which makes it clear what the value is for
multiples = num_check_one(start=1)
total=sum(multiples)
print('Multiples:', multiples)
print('Total:', total)

# Again, fixed casing/spacing, and gave the
# parameter a meaningful name
def num_check_two(start):
    multiples = []
    # Again, for loop
    for num in range(start, 10001):
        # 0 is Falsey, so we can
        # just treat it as a bool value
        if not num % 5:
            print(num)
            multiples.append(num)
            # Got rid of the recursive call again
        # You were never incrementing `num2` here,
        # which is why your code got into an infinite
        # loop. Also why you should use `for` loops by
        # default

num_check_two(1)

【讨论】:

  • 谢谢!这很有帮助。
【解决方案2】:

numCheck1:由于所有 3和5的倍数也是 15的倍数,您可以简单地打印出range(0, 1001, 15)中的每个项目(按15计数)

numCheck2:range(0, 1001, 5)(按 5 计数)应该已经是 5 的所有倍数小于或等于 1000。

numCheck3:range(0, 1001, 3)(按 3 计数)与上述相同,只是 3 的倍数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2015-08-31
    • 2013-06-20
    • 1970-01-01
    • 2013-12-03
    • 2012-05-25
    • 1970-01-01
    相关资源
    最近更新 更多