Python operator. itemgetter (& #39; click & #39;) is used to define the function for obtaining the & #39; click & #39; item
B = operator. itemgetter (1) defines function B, which is used to obtain the value of the 1st Field passed into the list.
B can be used as the key of the sort function. As the basis for sorting.
Adn_app_data_map is a dictionary.
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'] +''
Csv. writelines (record)
The test is as follows:
>>> Import types
>>> Test = {'A': '1', 'B': '2', 'C': '3', 'D': '4 '}
>>> Print test. items ()
[('A', '1'), ('C', '3'), ('B', '2'), ('D ', '4')]
>>> For key, val in test. items ():
Print type (val );