0

如何从多个字典获取字典列表并需要从基本计算开始日期和结束日期

[{'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'}]

如果基本金额相等,我需要合并等量字典,start_date 是开始日期,end_date 是年终日期

举个例子

{'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': 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'}]
4

2 回答 2

1
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()]

输出

[{'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}]
于 2013-10-30T04:04:29.230 回答
0

您的数据结构对于此操作很奇怪;这是一种不同的方法:

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)
于 2013-10-30T04:30:10.623 回答