簡體   English   中英

如何從 Python 中的復雜列表中提取元素

[英]How to extract elements from a complex list in Python

我有一個這樣的列表:

ls = {
    'info': {
        'id': '237371250',
        'market': 'BCH-PERP',
        'future': 'BCH-PERP',
        'side': 'sell',
        'type': 'stop',
        'orderPrice': None,
        'triggerPrice': '104.2',
        'size': '83.33',
        'status': 'open',
        'createdAt': '2022-07-16T12:25:02.288247+00:00',
        'triggeredAt': None,
        'orderId': None,
        'error': None,
        'reduceOnly': True,
        'trailValue': None,
        'trailStart': None,
        'cancelledAt': None,
        'cancelReason': None,
        'retryUntilFilled': True,
        'orderType': 'market',
        'filledSize': '0.0',
        'avgFillPrice': None},
    'id': '237371250',
    'clientOrderId': None,
    'timestamp': 1657974302288,
    'datetime': '2022-07-16T12:25:02.288Z',
    'lastTradeTimestamp': None,
    'symbol': 'BCH/USD:USD',
    'type': 'stop',
    'timeInForce': None,
    'postOnly': None,
    'reduceOnly': True,
    'side': 'sell',
    'price': 104.2,
    'stopPrice': 104.2,
    'amount': 83.33,
    'cost': 0.0,
    'average': None,
    'filled': 0.0,
    'remaining': 83.33,
    'status': 'open',
    'fee': None,
    'trades': [],
    'fees': []}

我想從中提取104.2'triggerPrice'值。 我該怎么做呢?

我試過ls['triggerPrice']但這會返回錯誤。

任何有關這是哪種數據結構以及如何訪問其元素的信息都將不勝感激。

這種數據結構稱為字典dict ),它的工作方式與 JSON 相同,只是類型是 Pythonic。

例如:一個典型的 JSON 文件是這樣的

{
    "id": 2,
    "name": null,
    "is_allowed": false
}

在python中它會是這樣的

{
    "id": 2,
    "name": None,
    "is_allowed": False,
}

如您所見,僅更改了類型。

要訪問字典中的元素,只需輸入字典的名稱,例如ls ,然后使用鍵在字典中查找值: ls["id"]

您的問題的解決方案是ls["info"]["triggerPrice"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM