优化列表和词典创建的循环

时间:2017-03-22 09:31:40

标签: python optimization list-comprehension dictionary-comprehension

我目前正在尝试通过减少for循环并尝试实现列表/字典理解来优化以下代码,代码如下:

  marketid = (s['marketId'] for s in booksn)
  refrunner = {j['selectionId']:[j['runnerName'], i['marketStartTime']] for i in booksn for j in i['runners']}

我现在遇到的问题是,我不确定如何删除第一个for循环并将“marketid”列表集成到“refrunner”字典中。

这些变量的一些背景是“marketid”用于向Web API发送参数列表,API需要一个列表。

需要“refrunner”字典,以便我可以将“runnerName”和“marketStartTime”与API响应的输出结合起来。 API响应返回一个字典列表,然后将这些字典与“refrunner”中的“selectionId”键进行协调,这样我就可以扩展每个“refrunner”键的值。

为避免疑问,下面的剪辑是“booksn”列表的摘录:

booksn = [{'marketId': '1.130412225', 'runners': [{'runnerName': 'Chirico Vallis', 'handicap': 0.0, 'sortPriority': 1, 'selectionId': 12128886}, {'runnerName': 'Kings War', 'handicap': 0.0, 'sortPriority': 2, 'selectionId': 11835260}, {'runnerName': 'Banjo', 'handicap': 0.0, 'sortPriority': 3, 'selectionId': 11835256}, {'runnerName': 'Jeremys Joy', 'handicap': 0.0, 'sortPriority': 4, 'selectionId': 8534820}, {'runnerName': 'Arena Seven', 'handicap': 0.0, 'sortPriority': 5, 'selectionId': 10950918}, {'runnerName': 'Private Ledger', 'handicap': 0.0, 'sortPriority': 6, 'selectionId': 12716038}, {'runnerName': 'Sir Issac', 'handicap': 0.0, 'sortPriority': 7, 'selectionId': 8930769}, {'runnerName': 'Pats Pearl', 'handicap': 0.0, 'sortPriority': 8, 'selectionId': 11103454}, {'runnerName': 'Dalmarella Dancer', 'handicap': 0.0, 'sortPriority': 9, 'selectionId': 7652544}, {'runnerName': 'Darcy Mcgees', 'handicap': 0.0, 'sortPriority': 10, 'selectionId': 12588978}, {'runnerName': 'Bank Balance', 'handicap': 0.0, 'sortPriority': 11, 'selectionId': 12183394}, {'runnerName': 'Royal Roslea', 'handicap': 0.0, 'sortPriority': 12, 'selectionId': 9069976}, {'runnerName': 'Lowertown Scholar', 'handicap': 0.0, 'sortPriority': 13, 'selectionId': 11385072}], 'totalMatched': 40927.82, 'marketStartTime': datetime.datetime(2017, 3, 19, 14, 40), 'marketName': '2m Mdn Hrd'}, {'marketId': '1.130412675', 'runners': [{'runnerName': 'Spanish Captain', 'handicap': 0.0, 'sortPriority': 1, 'selectionId': 10425181}, {'runnerName': 'Flying Rock', 'handicap': 0.0, 'sortPriority': 2, 'selectionId': 11569368}, {'runnerName': 'Hooponopono', 'handicap': 0.0, 'sortPriority': 3, 'selectionId': 7591031}, {'runnerName': 'Tuscan', 'handicap': 0.0, 'sortPriority': 4, 'selectionId': 6814235}, {'runnerName': 'Elusive Wolf', 'handicap': 0.0, 'sortPriority': 5, 'selectionId': 10589242}, {'runnerName': 'Master Of Mischief', 'handicap': 0.0, 'sortPriority': 6, 'selectionId': 9618022}, {'runnerName': 'Wandies', 'handicap': 0.0, 'sortPriority': 7, 'selectionId': 9121584}, {'runnerName': 'Fantasy Art', 'handicap': 0.0, 'sortPriority': 8, 'selectionId': 8913422}, {'runnerName': 'Founding Father', 'handicap': 0.0, 'sortPriority': 9, 'selectionId': 11324260}], 'totalMatched': 646.28, 'marketStartTime': datetime.datetime(2017, 3, 19, 14, 40), 'marketName': 'R7 1400m Hcap'}, {'marketId': '1.130413940', 'runners': [{'runnerName': '1. Markazi', 'handicap': 0.0, 'sortPriority': 1, 'selectionId': 11587869}, {'runnerName': '2. Platon', 'handicap': 0.0, 'sortPriority': 2, 'selectionId': 11144002}, {'runnerName': '3. Pando', 'handicap': 0.0, 'sortPriority': 3, 'selectionId': 12723254}, {'runnerName': '4. Mitchouka', 'handicap': 0.0, 'sortPriority': 4, 'selectionId': 12514813}, {'runnerName': '5. Joburg', 'handicap': 0.0, 'sortPriority': 5, 'selectionId': 12723252}, {'runnerName': '6. Yuman', 'handicap': 0.0, 'sortPriority': 6, 'selectionId': 12723251}, {'runnerName': '7. A Head Ahead', 'handicap': 0.0, 'sortPriority': 7, 'selectionId': 12723253}], 'totalMatched': 8.66, 'marketStartTime': datetime.datetime(2017, 3, 19, 14, 45), 'marketName': 'R4 1600m Listed'}, {'marketId': '1.130412611', 'runners': [{'runnerName': 'Mon Lino', 'handicap': 0.0, 'sortPriority': 1, 'selectionId': 10991116}, {'runnerName': 'Dawn Raider', 'handicap': 0.0, 'sortPriority': 2, 'selectionId': 125359}, {'runnerName': 'Kilcrea Bridge', 'handicap': 0.0, 'sortPriority': 3, 'selectionId': 10914090}, {'runnerName': 'Theatre Wine', 'handicap': 0.0, 'sortPriority': 4, 'selectionId': 10936323}, {'runnerName': 'Alkaa Lion', 'handicap': 0.0, 'sortPriority': 5, 'selectionId': 9483284}, {'runnerName': 'Sunken Treasure', 'handicap': 0.0, 'sortPriority': 6, 'selectionId': 11312993}, {'runnerName': 'Graineyhill', 'handicap': 0.0, 'sortPriority': 7, 'selectionId': 11562460}], 'totalMatched': 31926.2, 'marketStartTime': datetime.datetime(2017, 3, 19, 14, 50), 'marketName': '2m6f Mdn Hrd'}]

我对列表/字典理解很陌生,我正在努力学习如何以最小的开销获得这些信息。

0 个答案:

没有答案