我有一個字典,其中包含一個如下所示的串列:
{'subscription_id_list': ['92878351', '93153640', '93840845', '93840847'.......]
每次呼叫 API 時,串列中最多會有 10,000 個條目。我想要做的是回圈并附加串列,以便完成后,我的 {'subscription_id_list'} 包含所有內容。
有什么想法嗎?
uj5u.com熱心網友回復:
聽起來你有類似的東西
for api_request in some_structure:
response = get_remote_results(api_request)
# response structure is single-key dict,
# key is 'subscription_id_list'; value is list of str of int
# i.e. {'subscription-id-list': ['1234','5678',....]}
并且您想要一些完整的dict來包含list各種回應中的所有值。然后使用list.extend:
complete_sub_id_list = []
for api_request in some_structure:
response = get_remote_results(api_request)
complete_sub_id_list.extend(response['subscription_id_list'])
如評論中所述,如果您向遠程服務器或資源(甚至依賴于本地 I/O 的東西)發出 API 請求,您可能會想要使用異步呼叫;如果您還不熟悉這意味著什么,也許aiohttp和/或asyncio會很有幫助。有關更多資訊,請參閱例如此 Twilio 指南。 TL;DR與使用同步呼叫相比,您可以看到 5-30 倍的加速。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/355731.html
