简体   繁体   English

使python代码更高效

[英]Making python code more efficient

I have the code 我有代码

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

As a basic summary of my code, I have 3 functions, and corresponding lists and variables for each of them. 作为我的代码的基本摘要,我有3个函数,以及每个函数的对应列表和变量。 The first function checks for all multiples of 3 and 5 below and equal to 1000. The second checks for all multiples of 5 below and equal to 1000. The third checks for all multiples of 3 below and equal to 1000. The numbers that are multiples are added to the corresponding list, while the corresponding variable is incremented to allow the function to check all numbers. 第一个函数检查3以下和5等于1000的所有倍数。第二个函数检查5以下和等于1000的所有倍数。第三个函数检查3以下且等于1000的所有倍数。将被添加到相应的列表,同时增加相应的变量以允许函数检查所有数字。 At the end, the program calculates 4 totals: the total of each of the lists, and a special total, which adds together the second two totals and subtracts the first to prevent overcounting. 最后,程序将计算4个总数:每个列表的总数,以及一个特殊的总数,该总数将后两个总数相加并减去第一个总数以防止计数过多。 This is just the overarching structure of the program. 这只是程序的总体结构。

This program is supposed to solve this problem (not homework, just fun). 这个程序应该解决这个问题 (不是功课,只是很有趣)。 The code is working (as far as I know; the first function definitely works) but it keeps crashing the compiler (I am using an online compiler, repl . I am wondering if there are any ways to make this code more efficient. 代码可以正常工作(据我所知;第一个函数肯定可以正常工作),但是它一直使编译器崩溃(我使用的是在线编译器,请参见) ,我想知道是否有任何方法可以使此代码更有效。

Thanks! 谢谢!

I've re-written your code, with comments in-line. 我重新编写了您的代码,并添加了注释。 You could improve things even more over this, but I think seeing where you could improve this code will be helpful for you. 您可以在此基础上进一步改进,但是我认为查看可以在何处改进此代码将对您有所帮助。

# 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)

numCheck1 : Since all multiples of 3 and 5 are also multiples of 15, you can simply print out each item in range(0, 1001, 15) (counts by 15) numCheck1 :由于3和5的所有倍数都是15的倍数,因此您可以简单地打印出range(0, 1001, 15) numCheck1 range(0, 1001, 15) (以15计)的每个项目

numCheck2 : range(0, 1001, 5) (counts by 5) should already be all multiples of 5 less than or equal to 1000. numCheck2range(0, 1001, 5) numCheck2 range(0, 1001, 5)range(0, 1001, 5)计数)应该已经是5的所有整数倍,小于或等于1000。

numCheck3 : range(0, 1001, 3) (counts by 3) is the same thing as above, simply with multiples of 3. numCheck3range(0, 1001, 3) numCheck3 range(0, 1001, 3)range(0, 1001, 3)计数)与上述相同,只是3的倍数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM