0

我的一个观点中有以下代码。问题是我不知道如何将它重构为一个 for 循环。唯一的区别是“项目”。部分。我需要在同一个模板中的所有三个变量值,所以我认为我不能从 urls.py 传递信息。关于如何重写这个的任何想法,所以我不会重复自己?

谢谢。

def average(request):

    cal_list = []
    cal_list2 = []
    cal_list3 = []

    exams = Test.objects.filter(test__test_name__iexact="one")
    for item in exams:
        cal_list.append(int(item.start))
        result = sum(cal_list) / float(165) * 100
        result = result / len(cal_list)
        result = int(round(result))
        result = str(result) + '%'

    for item in exams:
        cal_list2.append(int(item.s1))
        result2 = sum(cal_list2) / float(165) * 100
        result2 = result2 / len(cal_list2)
        result2 = int(round(result2))
        result2 = str(result2) + '%'

    for item in exams:
        cal_list3.append(int(item.s2))
        result3 = sum(cal_list3) / float(165) * 100
        result3 = result3 / len(cal_list3)
        result3 = int(round(result3))
        result3 = str(result3) + '%'

    return direct_to_template(request, 'a.html', {'result': result, 'result2': result2, 'result3': result3})
4

4 回答 4

1

在您的循环中,您不断覆盖该results值,因此您可以推断该部分:

for item in items:
   cal_list.append(int(item.start))
   cal_list2.append(int(item.s1))
   cal_list3.append(int(item.s2))

result = "%s%%" % round((sum(cal_list) / float(165) * 100) / len(cal_list))
result2 = "%s%%" % round((sum(cal_list2) / float(165) * 100) / len(cal_list2))
result3 = "%s%%" % round((sum(cal_list3) / float(165) * 100) / len(cal_list3))
于 2012-10-14T04:13:02.480 回答
1

我只想创建一个函数:

def calculate_result(items):
    items = tuple(int(i) for i in items)
    result = sum(items) / 16500.0
    result = result / len(items)
    return '%i' % round(result) + '%'

result0 = calculate_result(item.start for item in exams)
result1 = calculate_result(item.s1 for item in exams)
result2 = calculate_result(item.s2 for item in exams)
于 2012-10-14T04:13:38.063 回答
0

您可以使用aggregate来在数据库层执行求和。

from django.db import models

def average(request):
    queryset = Test.objects.filter(test__test_name__iexact="one")
    total = queryset.count()
    results = queryset.aggregate(
        result1=models.Sum('start'),
        result2=models.Sum('s1'),
        result3=models.Sum('s2'))
    def transform(resultsum):
        result = resultsum / float(165) * 100
        result = result / total
        result = int(round(result))
        result = str(result) + '%'
        return result
    result1 = transform(results['result1'])
    result2 = transform(results['result2'])
    result3 = transform(results['result3'])
    return direct_to_template(request, 'a.html', {'result': result1, 'result2': result2, 'result3': result3})
于 2012-10-14T04:37:14.647 回答
0

在那里,您可以进行最简单的重构,它使循环免于仅对所需事物进行迭代并将计算移出循环。

def average(request):

    cal_list = []
    cal_list2 = []
    cal_list3 = []

    exams = Test.objects.filter(test__test_name__iexact="one")
    for item in exams:
        cal_list.append(int(item.start))
        cal_list2.append(int(item.s1))
        cal_list3.append(int(item.s2))

    result = sum(cal_list) / float(165) * 100
    result = result / len(cal_list)
    result = int(round(result))
    result = str(result) + '%'

    result2 = sum(cal_list2) / float(165) * 100
    result2 = result2 / len(cal_list2)
    result2 = int(round(result2))
    result2 = str(result2) + '%'

    result3 = sum(cal_list3) / float(165) * 100
    result3 = result3 / len(cal_list3)
    result3 = int(round(result3))
    result3 = str(result3) + '%'

    return direct_to_template(request, 'a.html', {'result': result, 'result2': result2, 'result3': result3})
于 2012-10-14T04:09:23.920 回答