如何使此代码更高效,更有效?

时间:2019-04-29 08:00:41

标签: python

我的代码太长了,我需要在许多方法中使用它。我认为绝对有更好的书写方式。但是我很难找到它。该代码的作用是:

  1. 取12个数字
  2. 它将它们输入到列表中(让我们称之为计数)
  3. 创建12个星星变量

它需要创建某种如下所示的矩阵:

*******     count1
********    count2
********** maxcount (could be any count from count1 to 12, as long as it is the maximum
*****      count5
etc..

因此,为了解释这一点,我们有12个计数变量,我需要采用其中的最大变量,因此将它们放在列表中。将它们放入列表后,我选择了max(count)以建立我的星号表示形式。

最多可能有10个星号。因此,在这12个计数中,最大的应该是旁边有10个星,而其他所有星都相对有它们的星。

这是我的代码,但对我而言似乎并不是最佳选择,它创建和初始化了很多变量,并且需要一些时间。

    count = list()
    count.append(count1)
    count.append(count2)
    count.append(count3)
    count.append(count4)
    count.append(count5)
    count.append(count6)
    count.append(count7)
    count.append(count8)
    count.append(count9)
    count.append(count10)
    count.append(count11)
    count.append(count12)

    stars1 = 0
    stars2 = 0
    stars3 = 0
    stars4 = 0
    stars5 = 0
    stars6 = 0
    stars7 = 0
    stars8 = 0
    stars9 = 0
    stars10 = 0
    stars11 = 0
    stars12 = 0

    stars1 = int((count1 * 10) / max(count))
    stars2 = int((count2 * 10) / max(count))
    stars3 = int(count3 * 10 / max(count))
    stars4 = int(count4 * 10 / max(count))
    stars5 = int(count5 * 10 / max(count))
    stars6 = int(count6 * 10 / max(count))
    stars7 = int(count7 * 10 / max(count))
    stars8 = int(count8 * 10 / max(count))
    stars9 = int(count9 * 10 / max(count))
    stars10 = int(count10 * 10 / max(count))
    stars11 = int(count11 * 10 / max(count))
    stars12 = int(count12 * 10 / max(count))

    astericks1 = ""
    astericks2 = ""
    astericks3 = ""
    astericks4 = ""
    astericks5 = ""
    astericks6 = ""
    astericks7 = ""
    astericks8 = ""
    astericks9 = ""
    astericks10 = ""
    astericks11 = ""
    astericks12 = ""

    for i in range(1, 11):
         if (i <= stars1):
             astericks1 += "*"
         else:
             astericks1 += " "
         if (i <= stars2):
             astericks2 += "*"
         astericks2 += " "
         if (i <= stars3):
             astericks3 += "*"
         else:
             astericks3 += " "
         if (i <= stars4):
             astericks4 += "*"
         else:
             astericks4 += " "
         if (i <= stars5):
             astericks5 += "*"
         else:
             astericks5 += " "
         if (i <= stars6):
             astericks6 += "*"
         else:
             astericks6 += " "
         if (i <= stars7):
             astericks7 += "*"
         else:
             astericks7 += " "
         if (i <= stars8):
             astericks8 += "*"
         else:
             astericks8 += " "
         if (i <= stars9):
             astericks9 += "*"
         else:
             astericks9 += " "
         if (i <= stars10):
             astericks10 += "*"
         else:
             astericks10 += " "
         if (i <= stars11):
             astericks11 += "*"
         else:
             astericks11 += " "
         if (i <= stars12):
             astericks12 += "*"
         else:
             astericks12 += " "

2 个答案:

答案 0 :(得分:4)

首先,这种if,elif语句和变量创建确实是不好的编码实践。其次,作为一点建议,您应该始终认为,如果出于相同的原因而拥有两个不同的变量,则需要使用列表。

据我从您的代码中了解,您最多可以打印10个星星作为最大值。因此,以下代码应使用列表推导产生与您相同的行为。我还要提到它可以更有效地编写,但是我想对您来说清楚而简单。

def form_valid(self, form):
    student = form.save(commit=False)
    #retrieve the current logged_in teacher, of course you have to be sure this view is only accesible for teachers (in dispatch for exemple)
    self.object.classteacher = self.request.user.teacher
    self.object.save()
    return super(StudentCreate, self).form_vaild(form)

#bonus the dispatch
def dispatch(self, request, *args, **kwargs):
        #get the loged in user
        if request.user.teacher:
            return super(StudentCreate, self).dispatch(request, *args, **kwargs)
        else:
            raise Http404

答案 1 :(得分:0)

很简单:

# Get 12 numbers as input
s = input("Input 12 numbers separated by a white space")

# Save them in a list and calculate the maximum
nums = [int(item) for item in s.split()]
max_num = max(nums)

for idx, item in enumerate(nums):
    # Calcuate number of stars, ensuring number of stars are never more then 10
    num_stars = int(item * 10 / max_num)

    # print the stars
    print('*' * num_stars, 'count{}'.format(idx))

样品输出为

Input 12 numbers separated by a white space1 2 3 4 5 6 1 2 3 4 5 6
* count0
*** count1
***** count2
****** count3
******** count4
********** count5
* count6
*** count7
***** count8
****** count9
******** count10
********** count11
相关问题