b = operator.itemgetter(1) 定义函数b,用于获取传入list的第1域的值
可以将b用于sort函数的key。作为排序的依据。
adn_app_data_map是个字典
for key, app_arr in adn_app_data_map.items():
app_arr.sort(key=operator.itemgetter(‘click‘), reverse=True)
app_arr = app_arr[:3]
keys = key.split(‘#‘)
category = keys[0]
ad_network_id = keys[1]
ad_id = keys[2]
for app in app_arr:
dimension_values = category + ‘#‘ + app[‘app_id‘] + ‘#‘ + ad_network_id + ‘#‘ + ad_id
record = app[‘campaign_id‘] + ‘,‘ + adn_ad_category_app_report + ‘,‘ + dimension_values + ‘,‘ + app[‘impression‘] + \
‘,‘ + str(app[‘click‘]) + ‘,‘ + app[‘impression_cost‘] + ‘,‘ + app[‘click_cost‘] + ‘\n‘
csv.writelines(record)
python的operator.itemgetter('click')用于定义获取'click'项的函数
原文地址:http://blog.csdn.net/longshenlmj/article/details/42711895