Merge lists while showing zeros in python

2019-08-18 02:44发布

问题:

I created a Django QuerySet which count values but unfortunately it do not show 0 values. Therefore I want to merge my two list like left join in SQL. I show my inputs and desired outputs .

INPUT 1 (Django Query 1):

I print it out like this:

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

INPUT 2 (Django Query 2):

I print it out like this:

    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

My desired output would be:

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 

I tried this but I failed:

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

回答1:

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"]


回答2:

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"

output:

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


回答3:

I tried to use pandas, which I think the easiest method. The steps of my method was:

1, Convert objects to DataFrame

  • pd_query_1 = pd.DataFrame.from_records(query_1)

  • pd_query_2 = pd.DataFrame.from_records(query_2)

2, Left join the two query

  • result = (pd.merge(pd_query_1, pd_query_2, how='left', on=['day', 'day'])).fillna(0)