简体   繁体   中英

Dictionary with two items with the same name

I am trying to retrieve the 'price' for the 'WIN_ODDS_BSP' so '10.09'. I am not sure if 'productType' is a dictionary or an item. If it is an item, how do I access the 'markets' dictionary where there are two items with the same name ('productType')?

Here is a sample of the json:

"markets":[{"productType":"WIN_ODDS_BSP","price":10.09,"reductionFactor":8.33},{"productType":"PLACE_ODDS_BSP","price":2.92,"reductionFactor":13.85}],"bestNZTote":0},

Here is my code:

for runner in json2['runners']:
    for market in runner['markets']:
        data.append((runner['runnerName'],runner.get('placedResult'),market.get('price'),runner.get('bestTote')))

Just pass an 'if' condition to each of the items in markets .

for market in runner['markets']:
  if market['productType'] == 'WIN_ODDS_BSP':
    data.append(market['price'])

.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM