g.p.karthick
g.p.karthick

Reputation: 1

how to calculate startdate and enddate from basic from multiple dictionaries to list of dictionary

How to get from multiple dictionary to list of dictionary and need to calculate startdate and enddate from basic

[{'basic': 1000.0, 'end_date': '2011-01-31', 'start_date': '2011-01-01'}, 
 {'basic': 1000.0, 'end_date': '2011-05-31', 'start_date': '2011-05-01'}, 
 {'basic': 2000.0, 'end_date': '2012-01-31', 'start_date': '2012-01-01'}, 
 {'basic': 2000.0, 'end_date': '2012-05-31', 'start_date': '2012-05-01'},
 {'basic': 2500.0, 'end_date': '2012-07-31', 'start_date': '2012-07-01'},
 {'basic': 2500.0, 'end_date': '2012-08-31', 'start_date': '2012-08-01'},
 {'basic': 3500.0, 'end_date': '2013-01-31', 'start_date': '2013-01-01'}, 
 {'basic': 3500.0, 'end_date': '2013-02-28', 'start_date': '2013-02-01'},
 {'basic': 4000.0, 'end_date': '2013-11-30', 'start_date': '2013-11-01'}]

if the basic amount equal i need to combine the equal amount dictionaries and start_date is begin date and end_date is end of year date

say for an example

{'basic': 1000.0, 'end_date': '2011-01-31', 'start_date': '2011-01-01'}, 
{'basic': 1000.0, 'end_date': '2011-05-31', 'start_date': '2011-05-01'}, 

two dictionary equal so i need to get like this

[{'basic': 1000.0, 'start_date': '2011-01-01', 'end_date': '2011-12-31',},
 {'basic': 2000.0, 'start_date': '2012-01-01', 'end_date': '2012-06-31', },
 {'basic': 2500.0, 'start_date': '2012-07-01', 'end_date': '2012-12-31',}, 
 {'basic': 3500.0, 'start_date': '2013-01-01', 'end_date': '2013-10-31'}, 
 {'basic': 4000.0, 'start_date': '2013-11-01', 'end_date': '2013-12-30'}]

Upvotes: 0

Views: 107

Answers (2)

Burhan Khalid
Burhan Khalid

Reputation: 174662

Your data structure is strange for this operation; here is a different approach:

d = {}

for i in dates:
  if not i['basic'] in d:
     d[i['basic']] = {}
     d[i['basic']]['start_date'] = []
     d[i['basic']]['end_date'] = []
  d[i['basic']]['start_date'].append(int(i['start_date'].replace('-','')))
  d[i['basic']]['end_date'].append(int(i['end_date'].replace('-','')))

# This will give you:

>>> d
{1000.0: {'start_date': [20110101, 20110501], 'end_date': [20110131, 20110531]},
 2000.0: {'start_date': [20120101, 20120501], 'end_date': [20120131, 20120531]},
 3500.0: {'start_date': [20130101, 20130201], 'end_date': [20130131, 20130228]},
 2500.0: {'start_date': [20120701, 20120801], 'end_date': [20120731, 20120831]},
 4000.0: {'start_date': [20131101], 'end_date': [20131130]}}

# Next, sort the start and end dates. We want the earliest start date,
# and the last end date for each number

for i in d.values():
   i['start_date'].sort()
   i['end_date'].sort(reverse=True)

# Now we have:

>>> d
{1000.0: {'start_date': [20110101, 20110501], 'end_date': [20110531, 20110131]},
 2000.0: {'start_date': [20120101, 20120501], 'end_date': [20120531, 20120131]},
 3500.0: {'start_date': [20130101, 20130201], 'end_date': [20130228, 20130131]},
 2500.0: {'start_date': [20120701, 20120801], 'end_date': [20120831, 20120731]},
 4000.0: {'start_date': [20131101], 'end_date': [20131130]}}

# To build our final result; we need to have for each
# number, the earliest start date, and the latest end date (which are the first
# items of each `start_date` and `end_date` dictionary keys

# Then we just do some formatting to get it back in to the date string

result = []
for i,v in d.iteritems():
   j = {}
   j['basic'] = i

   # Convert the start and end dates into strings
   # and format them
   start = str(v['start_date'][0])
   end = str(v['end_date'][0])

   j['start_date'] = '{0}-{1}-{2}'.format(start[:4],start[4:6],start[-2:])
   j['end_date'] = '{0}-{1}-{2}'.format(end[:4],end[4:6],end[-2:])

   result.append(j)

Upvotes: 0

thefourtheye
thefourtheye

Reputation: 239573

import datetime
def convertDate(dateString):
    return datetime.datetime.strptime(dateString, "%Y-%m-%d").date()

dates = [{'basic': 1000.0, 'end_date': '2011-01-31', 'start_date': '2011-01-01'}, 
 {'basic': 1000.0, 'end_date': '2011-05-31', 'start_date': '2011-05-01'}, 
 {'basic': 2000.0, 'end_date': '2012-01-31', 'start_date': '2012-01-01'}, 
 {'basic': 2000.0, 'end_date': '2012-05-31', 'start_date': '2012-05-01'},
 {'basic': 2500.0, 'end_date': '2012-07-31', 'start_date': '2012-07-01'},
 {'basic': 2500.0, 'end_date': '2012-08-31', 'start_date': '2012-08-01'},
 {'basic': 3500.0, 'end_date': '2013-01-31', 'start_date': '2013-01-01'}, 
 {'basic': 3500.0, 'end_date': '2013-02-28', 'start_date': '2013-02-01'},
 {'basic': 4000.0, 'end_date': '2013-11-30', 'start_date': '2013-11-01'}]

tempResult = {}
for dateDict in dates:
    if dateDict["basic"] in tempResult:
        if convertDate(tempResult[dateDict["basic"]]["end_date"]) < convertDate(dateDict["end_date"]):
            tempResult[dateDict["basic"]]["end_date"] = dateDict["end_date"]
        if convertDate(tempResult[dateDict["basic"]]["start_date"]) > convertDate(dateDict["start_date"]):
            tempResult[dateDict["basic"]]["start_date"] = dateDict["start_date"]
    else:
        tempResult[dateDict["basic"]] = dateDict
print [value for _, value in tempResult.items()]

Output

[{'start_date': '2011-01-01', 'end_date': '2011-05-31', 'basic': 1000.0},
 {'start_date': '2012-01-01', 'end_date': '2012-05-31', 'basic': 2000.0},
 {'start_date': '2013-01-01', 'end_date': '2013-02-28', 'basic': 3500.0},
 {'start_date': '2012-07-01', 'end_date': '2012-08-31', 'basic': 2500.0},
 {'start_date': '2013-11-01', 'end_date': '2013-11-30', 'basic': 4000.0}]

Upvotes: 1

Related Questions