如果基于存储的条件,python两个字典动态地for循环

时间:2018-03-12 07:13:13

标签: django python-3.x

db根据生成的for循环

获取数据密钥
def data(request):
    from_date=2009-01-01
    todate=2009-01-31
    with connection.cursor() as cursor:
         cursor.execute("select id,name,no_of_apsent,no_of_lop,amount from pay 
                        where from_date='%s' and todate='%s'"%from_date,todate)
        fetched_data=cursor.fectall()
        a={}
        b={}
        for c,d,e,f,g in fetched_data:
                if f<=e:
                    h=g*10/100
                    a[c]=h
                else:
                    i=g/31
                    b[c]=i
        print(a,b)
    return render(request,hai.html,{'a'a,'b',b})

db根据生成的for循环获取数据密钥 例如,如果条件为真,则键值基于存储 {0:1,1:0,2:9} 像这样 如果键值1为假,则键值存储0为真意味着存储所需值。这是麻烦

1 个答案:

答案 0 :(得分:0)

def data(request):
from_date=2009-01-01
todate=2009-01-31
with connection.cursor() as cursor:
     cursor.execute("select id,name,no_of_apsent,no_of_lop,amount from pay 
                     where from_date='%s' and todate='%s'"%from_date,todate)
     fetched_data=cursor.fectall()
     a={}
     b={}
     for c,d,e,f,g in fetched_data:
           if f<=e:
              h=g*10/100
              a[c]=h
              b[c]=0
           else:
              i=g/31
              b[c]=i
              a[c]=0
     print(a,b)
return render(request,hai.html,{'a'a,'b',b})