python - Nested Lists 和迭代删除

我想迭代这个 list

[{'quote': {'USD': {'close': 50861.552205896805,
    'high': 50982.038774035216,
    'low': 50730.75579185529,
    'market_cap': 961892403779.12,
    'open': 50881.508801727796,
    'timestamp': '2021-12-27T02:59:59.999Z',
    'volume': 20597797622.55}}},
 {'quote': {'USD': {'close': 51020.884322626065,
    'high': 51053.01734838643,
    'low': 50875.17580190205,
    'market_cap': 964908852082.23,
    'open': 51031.607414525155,
    'timestamp': '2021-12-27T04:59:59.999Z',
    'volume': 20155251492.28}}},
 {'quote': {'USD': {'close': 50961.30660215557,
    'high': 50968.54723757928,
    'low': 50787.682527382436,
    'market_cap': 963786906391.14,
    'open': 50872.72513079699,
    'timestamp': '2021-12-27T06:59:59.999Z',
    'volume': 20373929446.42}}}]

删除领先的报价和美元信息;

[{'close': 50861.552205896805,
    'high': 50982.038774035216,
    'low': 50730.75579185529,
    'market_cap': 961892403779.12,
    'open': 50881.508801727796,
    'timestamp': '2021-12-27T02:59:59.999Z',
    'volume': 20597797622.55},
 {'close': 51020.884322626065,
    'high': 51053.01734838643,
    'low': 50875.17580190205,
    'market_cap': 964908852082.23,
    'open': 51031.607414525155,
    'timestamp': '2021-12-27T04:59:59.999Z',
    'volume': 20155251492.28},
 {'close': 50961.30660215557,
    'high': 50968.54723757928,
    'low': 50787.682527382436,
    'market_cap': 963786906391.14,
    'open': 50872.72513079699,
    'timestamp': '2021-12-27T06:59:59.999Z',
    'volume': 20373929446.42}]

任何帮助将不胜感激!

回答1

您可以使用带有查找的 list 理解来删除每个条目的外部字典:

[item['quote']['USD'] for item in data]

这输出:

[
 {
  'close': 50861.552205896805,
  'high': 50982.038774035216,
  'low': 50730.75579185529,
  'market_cap': 961892403779.12,
  'open': 50881.508801727796,
  'timestamp': '2021-12-27T02:59:59.999Z',
  'volume': 20597797622.55
 },
 {
  'close': 51020.884322626065,
  'high': 51053.01734838643,
  'low': 50875.17580190205,
  'market_cap': 964908852082.23,
  'open': 51031.607414525155,
  'timestamp': '2021-12-27T04:59:59.999Z',
  'volume': 20155251492.28
 },
 {
  'close': 50961.30660215557,
  'high': 50968.54723757928,
  'low': 50787.682527382436,
  'market_cap': 963786906391.14,
  'open': 50872.72513079699,
  'timestamp': '2021-12-27T06:59:59.999Z',
  'volume': 20373929446.42
 }
]

回答2

只是为了添加到其他人的答案。

list = [{'quote': {'USD': {'close': 50861.552205896805,
    'high': 50982.038774035216,
    'low': 50730.75579185529,
    'market_cap': 961892403779.12,
    'open': 50881.508801727796,
    'timestamp': '2021-12-27T02:59:59.999Z',
    'volume': 20597797622.55}}},
 {'quote': {'USD': {'close': 51020.884322626065,
    'high': 51053.01734838643,
    'low': 50875.17580190205,
    'market_cap': 964908852082.23,
    'open': 51031.607414525155,
    'timestamp': '2021-12-27T04:59:59.999Z',
    'volume': 20155251492.28}}},
 {'quote': {'USD': {'close': 50961.30660215557,
    'high': 50968.54723757928,
    'low': 50787.682527382436,
    'market_cap': 963786906391.14,
    'open': 50872.72513079699,
    'timestamp': '2021-12-27T06:59:59.999Z',
    'volume': 20373929446.42}}}]


data = [item['quote']['USD'] for item in list]
print(data)

相似文章

java - 部署刷新接管项目的问题

当我尝试将我的war部署到tomcat时,我收到以下错误,对于可以提供的任何帮助,我都会非常感激。我提供了错误/堆栈跟踪和它似乎引用的源代码。任何可以提供修复源代码的帮助将不胜感激。如果您需要更多详细...

最新文章