我的数据格式如下:
{0: {'offer_id': u'308249', 'affiliate_id': u'1014', 'approval_status': 'approved'}, 1: {'offer_id': u'308255', 'affiliate_id': u'1014', 'approval_status': 'approved'}, 2: {'offer_id': u'283063', 'affiliate_id': u'1014', 'approval_status': 'approved'}, 3: {'offer_id': u'313467', 'affiliate_id': u'1014', 'approval_status': 'approved'}, 4: {'offer_id': u'313673', 'affiliate_id': u'1014', 'approval_status': 'approved'}, 5: {'offer_id': u'324519', 'affiliate_id': u'1014', 'approval_status': 'approved'}, 6: {'offer_id': u'320501', 'affiliate_id': u'1014', 'approval_status': 'approved'}, 7: {'offer_id': u'320503', 'affiliate_id': u'1014', 'approval_status': 'approved'}, 8: {'offer_id': u'323593', 'affiliate_id': u'1014', 'approval_status': 'approved'}, 9: {'offer_id': u'323595', 'affiliate_id': u'1014', 'approval_status': 'approved'}, 10: {'offer_id': u'324441', 'affiliate_id': u'1014', 'approval_status': 'approved'}, 11: {'offer_id': u'324443', 'affiliate_id': u'1014', 'approval_status': 'approved'}, 12: {'offer_id': u'325129', 'affiliate_id': u'1014', 'approval_status': 'approved'}, 13: {'offer_id': u'325131', 'affiliate_id': u'1014', 'approval_status': 'approved'}, 14: {'offer_id': u'325133', 'affiliate_id': u'1014', 'approval_status': 'approved'}, 15: {'offer_id': u'325135', 'affiliate_id': u'1014', 'approval_status': 'approved'}}
我需要一次只处理10个dicts 我需要将这个dict分成几部分,这样每个子部分最多可以有10个元素(键/值对)。
我需要进行的API调用最多可以采用这种格式的10个值,因此我需要逐个处理零件。
示例:
for data in data_in_chunks_of_10:
api_call(data)
我如何有效地实现这一目标?
答案 0 :(得分:1)
您可以使用生成器按照所需的块大小返回数据块:
def blocks(data, n=10):
for i in xrange(0, len(data), n):
yield data[i:i+n]
data = {
0: {'offer_id': u'308249', 'affiliate_id': u'1014', 'approval_status': 'approved'},
1: {'offer_id': u'308255', 'affiliate_id': u'1014', 'approval_status': 'approved'},
2: {'offer_id': u'283063', 'affiliate_id': u'1014', 'approval_status': 'approved'},
3: {'offer_id': u'313467', 'affiliate_id': u'1014', 'approval_status': 'approved'},
4: {'offer_id': u'313673', 'affiliate_id': u'1014', 'approval_status': 'approved'},
5: {'offer_id': u'324519', 'affiliate_id': u'1014', 'approval_status': 'approved'},
6: {'offer_id': u'320501', 'affiliate_id': u'1014', 'approval_status': 'approved'},
7: {'offer_id': u'320503', 'affiliate_id': u'1014', 'approval_status': 'approved'},
8: {'offer_id': u'323593', 'affiliate_id': u'1014', 'approval_status': 'approved'},
9: {'offer_id': u'323595', 'affiliate_id': u'1014', 'approval_status': 'approved'},
10: {'offer_id': u'324441', 'affiliate_id': u'1014', 'approval_status': 'approved'},
11: {'offer_id': u'324443', 'affiliate_id': u'1014', 'approval_status': 'approved'},
12: {'offer_id': u'325129', 'affiliate_id': u'1014', 'approval_status': 'approved'},
13: {'offer_id': u'325131', 'affiliate_id': u'1014', 'approval_status': 'approved'},
14: {'offer_id': u'325133', 'affiliate_id': u'1014', 'approval_status': 'approved'},
15: {'offer_id': u'325135', 'affiliate_id': u'1014', 'approval_status': 'approved'}}
for block in blocks(data.items(), 10):
print block
这将为您提供两个数据块,如下所示:
[(0, {'offer_id': u'308249', 'affiliate_id': u'1014', 'approval_status': 'approved'}), (1, {'offer_id': u'308255', 'affiliate_id': u'1014', 'approval_status': 'approved'}), (2, {'offer_id': u'283063', 'affiliate_id': u'1014', 'approval_status': 'approved'}), (3, {'offer_id': u'313467', 'affiliate_id': u'1014', 'approval_status': 'approved'}), (4, {'offer_id': u'313673', 'affiliate_id': u'1014', 'approval_status': 'approved'}), (5, {'offer_id': u'324519', 'affiliate_id': u'1014', 'approval_status': 'approved'}), (6, {'offer_id': u'320501', 'affiliate_id': u'1014', 'approval_status': 'approved'}), (7, {'offer_id': u'320503', 'affiliate_id': u'1014', 'approval_status': 'approved'}), (8, {'offer_id': u'323593', 'affiliate_id': u'1014', 'approval_status': 'approved'}), (9, {'offer_id': u'323595', 'affiliate_id': u'1014', 'approval_status': 'approved'})]
[(10, {'offer_id': u'324441', 'affiliate_id': u'1014', 'approval_status': 'approved'}), (11, {'offer_id': u'324443', 'affiliate_id': u'1014', 'approval_status': 'approved'}), (12, {'offer_id': u'325129', 'affiliate_id': u'1014', 'approval_status': 'approved'}), (13, {'offer_id': u'325131', 'affiliate_id': u'1014', 'approval_status': 'approved'}), (14, {'offer_id': u'325133', 'affiliate_id': u'1014', 'approval_status': 'approved'}), (15, {'offer_id': u'325135', 'affiliate_id': u'1014', 'approval_status': 'approved'})]