bidding_result对应下面的数据结构,要取近三年的bidCount,取出年份,过滤保留近3年的,再根据年份键取对应的值,因为年份键对应有多个子字典,因此需要调用get_val取子字典的键以及子字典的值:
CollectUtils.get_val(year, [biddingModel.Result.STATISTIC, biddingModel.Result.Year.bidCount],
0)
bidding_result对应下面的简略数据结构:
{u'201601':{
u'provinceDistribution': [{u'province': u'\u5317\u4eac\u5e02',u'winMoneyAmount': 1135000.0,...}],
u'statistic': {u'winMoneyAmount': 5917400.0, u'bidMoneyAmount': 17722400.0,...,u'bidCount': 5}},
...
}
实际结构:
{u'201601': {u'provinceDistribution': [{u'province': u'\u5317\u4eac\u5e02', u'winMoneyAmount': 1135000.0, u'bidMoneyAmount': 12940000.0, u'bidCount': 3, u'winCount': 2}, {u'province': u'\u6e56\u5357\u7701', u'winMoneyAmount': 356000.0, u'bidMoneyAmount': 356000.0, u'bidCount': 1, u'winCount': 1}, {u'province': u'\u6d59\u6c5f\u7701', u'winMoneyAmount': 4426400.0, u'bidMoneyAmount': 4426400.0, u'bidCount': 1, u'winCount': 1}], u'statistic': {u'winMoneyAmount': 5917400.0, u'bidMoneyAmount': 17722400.0, u'bidCount': 5, u'winCount': 4}},
u'201401': {u'provinceDistribution': [{u'province': u'\u5c71\u4e1c\u7701', u'winMoneyAmount': None, u'bidMoneyAmount': None, u'bidCount': 1, u'winCount': 1}, {u'province': u'\u5317\u4eac\u5e02', u'winMoneyAmount': 118000.0, u'bidMoneyAmount': 118000.0, u'bidCount': 1, u'winCount': 1}], u'statistic': {u'winMoneyAmount': 118000.0, u'bidMoneyAmount': 118000.0, u'bidCount': 2, u'winCount': 2}},
u'201501': {u'provinceDistribution': [{u'province': u'\u5317\u4eac\u5e02', u'winMoneyAmount': 3609600.0, u'bidMoneyAmount': 3609600.0, u'bidCount': 3, u'winCount': 3}, {u'province': u'\u6d59\u6c5f\u7701', u'winMoneyAmount': 67600000.0, u'bidMoneyAmount': 67600000.0, u'bidCount': 2, u'winCount': 2}], u'statistic': {u'winMoneyAmount': 71209600.0, u'bidMoneyAmount': 71209600.0, u'bidCount': 5, u'winCount': 5}},
u'201301': {u'provinceDistribution': [{u'province': u'\u5317\u4eac\u5e02', u'winMoneyAmount': 1546726.0, u'bidMoneyAmount': 1546726.0, u'bidCount': 1, u'winCount': 1}], u'statistic': {u'winMoneyAmount': 1546726.0, u'bidMoneyAmount': 1546726.0, u'bidCount': 1, u'winCount': 1}}}
获取特征脚本:
biddingModel.Result.YEAR = '2018'#设置的一个固定值;
1) @staticmethod
def __set_bidding_result(features, bidding_result):
"""
设置招投标
:type features FeaturesDto
"""
features.bidding_cnt = Stream(bidding_result.items()) \
.filter(lambda (key_year, _): biddingModel.Result.YEAR - int(str(key_year)[:4]) <= 3) \
.map(lambda (_, value_year): value_year) \
.flat_map(
lambda year: CollectUtils.get_val(year, [biddingModel.Result.STATISTIC, biddingModel.Result.Year.bidCount],
0)) \
.sum()
2)获取特定字典内的key对应的键值,contents是对应最外层的字典,key_or_key_list对应根据该键要取的值;
@staticmethod
def get_val(contents, key_or_key_list, default=None):
"""
返回指定子集的值
"""
if not contents:
return default
if isinstance(key_or_key_list, list):
for idx, key in enumerate(key_or_key_list):
# contents需要一直是dict类型(除了最后一个key的结果),不然无法get
if not isinstance(contents, dict) or contents == {}:
contents = default
break
contents = contents.get(key, default)
else: # 按传入key_list 为一单独字串处理
contents = contents.get(key_or_key_list, default)
# 最后一次get的结果需要不为None|''|[]|{},否则以default返回
if contents is None:
contents = default
return contents
apit通过Stream获取特定数据的值
最新推荐文章于 2023-03-03 15:21:49 发布