在python中显示零时合并列表

2024-04-20 00:44:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个Django查询集,它可以计算值,但不幸的是,它没有显示0值。因此,我想合并我的两个列表,就像SQL中的left join一样。我显示我的输入和期望的输出。你知道吗

输入1(Django查询1):

我这样打印出来:

for i in query_1:
     print (i['day'], i['count_1'])  

2018-01-17 00:00:00+01:00 49
2018-01-16 00:00:00+01:00 139
2018-01-15 00:00:00+01:00 144
2018-01-14 00:00:00+01:00 142
2018-01-13 00:00:00+01:00 141
2018-01-12 00:00:00+01:00 144
2018-01-11 00:00:00+01:00 145
2018-01-10 00:00:00+01:00 95

输入2(Django查询2):

我这样打印出来:

    for i in query_2:
         print (i['day'], i['count_2'])  

   2018-01-17 00:00:00+01:00 2
   2018-01-16 00:00:00+01:00 6
   2018-01-14 00:00:00+01:00 2
   2018-01-13 00:00:00+01:00 4

我想要的输出是:

2018-01-17 00:00:00+01:00 49  2
2018-01-16 00:00:00+01:00 139 6
2018-01-15 00:00:00+01:00 144 0
2018-01-14 00:00:00+01:00 142 2
2018-01-13 00:00:00+01:00 141 4
2018-01-12 00:00:00+01:00 144 0
2018-01-11 00:00:00+01:00 145 0
2018-01-10 00:00:00+01:00 95  0 

我试过了,但失败了:

for a, b in zip (query_1, query_2)
    if a['day'] == b['day']:
       print (a['count_2']) #It prints only the result of query_2 not both :(
    else:
       print ('0') 

Tags: djangoin列表forsqlifcountit
3条回答
query_1 = [{'day': '2018-01-17 00:00:00+01:00','count_1': '49'},
           {'day': '2018-01-16 00:00:00+01:00','count_1': '139'},
           {'day': '2018-01-15 00:00:00+01:00','count_1': '144'},
           {'day': '2018-01-14 00:00:00+01:00','count_1': '142'}]


query_2 = [{'day': '2018-01-17 00:00:00+01:00','count_2': '2'},
           {'day': '2018-01-16 00:00:00+01:00','count_2': '6'},
           {'day': '2018-01-15 00:00:00+03:00','count_2': '2'},
           {'day': '2018-01-14 00:00:00+01:00','count_2': '4'}]

for a, b in zip(query_1, query_2):
    if a['day'] == b['day']:
        print a['day'] + " " + a['count_1'] + " " + b['count_2']
    else:
        print a['day'] + " " + a['count_1'] + " 0"

输出:

2018-01-17 00:00:00+01:00 49 2
2018-01-16 00:00:00+01:00 139 6
2018-01-15 00:00:00+01:00 144 0
2018-01-14 00:00:00+01:00 142 4

我试着用熊猫,我认为这是最简单的方法。我的方法步骤是:

1,将对象转换为数据帧

  • pd_query_1 = pd.DataFrame.from_records(query_1)

  • pd_query_2 = pd.DataFrame.from_records(query_2)

2,左连接两个查询

  • result = (pd.merge(pd_query_1, pd_query_2, how='left', on=['day', 'day'])).fillna(0)
new_data = [ i['day'] + ' ' + str(i['count_1']) for i in query_1]  
new_data.extend([ i['day'] + ' ' + str(i['count_1']) for i in query_2])
local_dict = {}
for values in new_data:
    date, time, val = values.split()
    if date in local_dict:
        new_value = local_dict[date]
        local_dict[date] = new_value.replace(new_value[-1],val)
    else:
        local_dict.setdefault(date,values+ (' %s' % '0'))
print(local_dict.values())
>>>
["2018-01-17 00:00:00+01:00 49  2",
"2018-01-16 00:00:00+01:00 139 6",
"2018-01-15 00:00:00+01:00 144 0",
"2018-01-14 00:00:00+01:00 142 2",
"2018-01-13 00:00:00+01:00 141 4",
"2018-01-12 00:00:00+01:00 144 0",
"2018-01-11 00:00:00+01:00 145 0",
"2018-01-10 00:00:00+01:00 95  0"]

相关问题 更多 >