Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 访问列表中的字典中的值_Python_List_Dictionary_For Loop - Fatal编程技术网

Python 访问列表中的字典中的值

Python 访问列表中的字典中的值,python,list,dictionary,for-loop,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.15602

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

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)
 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
TypeError: list indices must be integers or slices, not dict
我已经研究了这个问题,并将我的代码调整为以下内容:

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

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

    print(x)
 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
TypeError: list indices must be integers or slices, not dict
如果有人能帮我进入市场[0]['runners'][0]['ex']['availableToBack'][0]['price'],对于所有12名使用for循环的跑步者来说,这将是非常棒的

干杯,
Sandy

在这样迭代时,“i”已经是dict的一个元素,而不是int。因此您应该:

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

编辑:我将“I”重命名为“runner”,以便更具可读性,正如Philipp F.所建议的那样。

您必须迭代一个数字或一个范围。既然你的字典中存储了跑步者的数量,为什么不使用这个呢

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

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

我认为,将i重命名为runner使代码更具可读性。这比我下面的答案要好得多。你说得对,Phillipp F,谢谢!我刚刚编辑了我的答案。应该是market\u book\u result[0]['numberOfRunners'],而不是market\u book\u result['numberOfRunners']