访问列表中字典中的值

时间:2019-03-11 12:19:22

标签: python list dictionary for-loop

我有以下列表,其中包含许多其他列表和词典:

market_book_result = [{'betDelay': 0,
  'bspReconciled': False,
  'complete': True,
  'crossMatching': True,
  'inplay': False,
  'isMarketDataDelayed': True,
  'lastMatchTime': '2019-03-11T11:40:44.078Z',
  'marketId': '1.156023330',
  'numberOfActiveRunners': 12,
  'numberOfRunners': 12,
  'numberOfWinners': 1,
  'runners': [{'adjustmentFactor': 62.5,
    'ex': {'availableToBack': [{'price': 1.42, 'size': 94.47},
      {'price': 1.41, 'size': 157.16},
      {'price': 1.4, 'size': 426.93}],
     'availableToLay': [{'price': 1.43, 'size': 52.36},
      {'price': 1.44, 'size': 166.83},
      {'price': 1.45, 'size': 99.06}],
     'tradedVolume': []},
    'handicap': 0.0,
    'lastPriceTraded': 1.44,
    'selectionId': 13079071,
    'status': 'ACTIVE',
    'totalMatched': 0.0},
   {'adjustmentFactor': 18.854,
    'ex': {'availableToBack': [{'price': 4.8, 'size': 18.28},
      {'price': 4.7, 'size': 18.45},
      {'price': 4.6, 'size': 22.06}],
     'availableToLay': [{'price': 5.0, 'size': 42.1},
      {'price': 5.1, 'size': 156.12},
      {'price': 5.2, 'size': 13.85}],
     'tradedVolume': []},
    'handicap': 0.0,
    'lastPriceTraded': 5.0,
    'selectionId': 13101666,
    'status': 'ACTIVE',
    'totalMatched': 0.0},
   {'adjustmentFactor': 4.702,
    'ex': {'availableToBack': [{'price': 23.0, 'size': 13.84},
      {'price': 20.0, 'size': 10.36},
      {'price': 19.0, 'size': 10.15}],
     'availableToLay': [{'price': 40.0, 'size': 18.11},
      {'price': 55.0, 'size': 55.07},
      {'price': 80.0, 'size': 24.04}],
     'tradedVolume': []},
    'handicap': 0.0,
    'lastPriceTraded': 29.0,
    'selectionId': 12412709,
    'status': 'ACTIVE',
    'totalMatched': 0.0},
   {'adjustmentFactor': 3.766,
    'ex': {'availableToBack': [{'price': 42.0, 'size': 14.78},
      {'price': 34.0, 'size': 19.57},
      {'price': 28.0, 'size': 10.28}],
     'availableToLay': [{'price': 50.0, 'size': 10.12},
      {'price': 80.0, 'size': 45.05},
      {'price': 90.0, 'size': 15.4}],
     'tradedVolume': []},
    'handicap': 0.0,
    'lastPriceTraded': 50.0,
    'selectionId': 15700821,
    'status': 'ACTIVE',
    'totalMatched': 0.0},
   {'adjustmentFactor': 2.853,
    'ex': {'availableToBack': [{'price': 27.0, 'size': 10.96},
      {'price': 22.0, 'size': 12.91},
      {'price': 21.0, 'size': 10.45}],
     'availableToLay': [{'price': 60.0, 'size': 12.13},
      {'price': 70.0, 'size': 20.02},
      {'price': 80.0, 'size': 35.05}],
     'tradedVolume': []},
    'handicap': 0.0,
    'lastPriceTraded': 38.0,
    'selectionId': 22652454,
    'status': 'ACTIVE',
    'totalMatched': 0.0},
   {'adjustmentFactor': 1.952,
    'ex': {'availableToBack': [{'price': 110.0, 'size': 12.61},
      {'price': 85.0, 'size': 13.66},
      {'price': 70.0, 'size': 11.09}],
     'availableToLay': [{'price': 730.0, 'size': 17.16},
      {'price': 1000.0, 'size': 2.38}],
     'tradedVolume': []},
    'handicap': 0.0,
    'lastPriceTraded': 250.0,
    'selectionId': 13264025,
    'status': 'ACTIVE',
    'totalMatched': 0.0},
   {'adjustmentFactor': 1.803,
    'ex': {'availableToBack': [{'price': 65.0, 'size': 11.25},
      {'price': 48.0, 'size': 14.92},
      {'price': 34.0, 'size': 19.39}],
     'availableToLay': [{'price': 95.0, 'size': 13.94},
      {'price': 140.0, 'size': 13.38},
      {'price': 230.0, 'size': 35.05}],
     'tradedVolume': []},
    'handicap': 0.0,
    'lastPriceTraded': 95.0,
    'selectionId': 13747442,
    'status': 'ACTIVE',
    'totalMatched': 0.0},
   {'adjustmentFactor': 1.43,
    'ex': {'availableToBack': [{'price': 370.0, 'size': 12.56},
      {'price': 140.0, 'size': 10.16},
      {'price': 100.0, 'size': 73.2}],
     'availableToLay': [{'price': 1000.0, 'size': 10.71}],
     'tradedVolume': []},
    'handicap': 0.0,
    'lastPriceTraded': 740.0,
    'selectionId': 20765364,
    'status': 'ACTIVE',
    'totalMatched': 0.0},
   {'adjustmentFactor': 1.148,
    'ex': {'availableToBack': [{'price': 130.0, 'size': 11.08},
      {'price': 95.0, 'size': 60.19},
      {'price': 65.0, 'size': 11.26}],
     'availableToLay': [{'price': 1000.0, 'size': 6.38}],
     'tradedVolume': []},
    'handicap': 0.0,
    'lastPriceTraded': 590.0,
    'selectionId': 15803027,
    'status': 'ACTIVE',
    'totalMatched': 0.0},
   {'adjustmentFactor': 0.369,
    'ex': {'availableToBack': [{'price': 1000.0, 'size': 30.95},
      {'price': 510.0, 'size': 16.54},
      {'price': 220.0, 'size': 82.29}],
     'availableToLay': [],
     'tradedVolume': []},
    'handicap': 0.0,
    'lastPriceTraded': 1000.0,
    'selectionId': 21632638,
    'status': 'ACTIVE',
    'totalMatched': 0.0},
   {'adjustmentFactor': 0.352,
    'ex': {'availableToBack': [{'price': 370.0, 'size': 13.25},
      {'price': 270.0, 'size': 22.52},
      {'price': 200.0, 'size': 13.92}],
     'availableToLay': [{'price': 980.0, 'size': 11.83},
      {'price': 1000.0, 'size': 10.81}],
     'tradedVolume': []},
    'handicap': 0.0,
    'lastPriceTraded': 940.0,
    'selectionId': 15715002,
    'status': 'ACTIVE',
    'totalMatched': 0.0},
   {'adjustmentFactor': 0.273,
    'ex': {'availableToBack': [{'price': 1000.0, 'size': 245.38},
      {'price': 600.0, 'size': 14.33},
      {'price': 500.0, 'size': 37.24}],
     'availableToLay': [],
     'tradedVolume': []},
    'handicap': 0.0,
    'lastPriceTraded': 1000.0,
    'selectionId': 22760633,
    'status': 'ACTIVE',
    'totalMatched': 0.0}],
  'runnersVoidable': False,
  'status': 'OPEN',
  'totalAvailable': 891400.06,
  'totalMatched': 56244.82,
  'version': 2681378822}]

我可以像这样访问每个名为price的键的值:

market_book_result[0]['runners'][0]['ex']['availableToBack'][0]['price']

market_book_result[0]['runners'][1]['ex']['availableToBack'][0]['price']

market_book_result[0]['runners'][2]['ex']['availableToBack'][0]['price']

我想使用for循环访问这些值。为此,我尝试了以下方法:

for i in market_book_result[0]['runners']:

    x = market_book_result[0]['runners'][i]['ex']['availableToBack'][0]['price']

    print(x)

但这会导致以下错误:

TypeError: list indices must be integers or slices, not dict

我查看了这个问题Python access dictionary inside list of a dictionary,并将代码调整为以下内容:

 for dic in market_book_result[0]['runners']:
        for key,value in market_book_result[0]['runners'][dic]['ex']['availableToBack'][0]['price'].iteritems():
         print(value)

但这会导致以下错误:

TypeError: list indices must be integers or slices, not dict

如果有人可以使用for循环帮助我为所有12位跑步者访问market_book_result[0]['runners'][0]['ex']['availableToBack'][0]['price'],那么这是很好的选择。

干杯, 桑迪

3 个答案:

答案 0 :(得分:4)

像这样迭代时,“ i”已经是流浪者的一个元素(一个字典,而不是一个整数)。所以你应该这样做:

for runner in market_book_result[0]['runners']:
    x = runner['ex']['availableToBack'][0]['price']
    print(x)

编辑:如“ Philipp F”所建议的那样,我将“ i”重命名为“跑步者”以提高可读性。

答案 1 :(得分:2)

尝试一下

please enter your name
Caleb
your name is Caleb

答案 2 :(得分:1)

您必须遍历一个数字或一个范围。由于字典中存储的跑步人数众多,为什么不使用它呢?

for i in range(market_book_result['numberOfRunners']):
  x = market_book_result[0]['runners'][i]['ex']['availableToBack'][0]['price']
  print(x)