如何在python中使用条件访问嵌套列表中的元素

2024-09-27 21:33:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个嵌套列表,需要根据条件筛选出一个键/值

名单如下:

[{'buyer': False,
  'commission': '0.01333920',
  'commissionAsset': 'USDT',
  'id': 52549382,
  'maker': False,
  'marginAsset': 'USDT',
  'orderId': 2448678953,
  'positionSide': 'BOTH',
  'price': '3334.80',
  'qty': '0.010',
  'quoteQty': '33.34800',
  'realizedPnl': '0',
  'side': 'SELL',
  'symbol': 'MKRUSDT',
  'time': 1628311810877},
 {'buyer': True,
  'commission': '0.01334440',
  'commissionAsset': 'USDT',
  'id': 52544760,
  'maker': False,
  'marginAsset': 'USDT',
  'orderId': 2447909783,
  'positionSide': 'BOTH',
  'price': '3336.10',
  'qty': '0.010',
  'quoteQty': '33.36100',
  'realizedPnl': '-0.29400000',
  'side': 'BUY',
  'symbol': 'MKRUSDT',
  'time': 1628309268672},
 {'buyer': False,
  'commission': '0.01322680',
  'commissionAsset': 'USDT',
  'id': 52532126,
  'maker': False,
  'marginAsset': 'USDT',
  'orderId': 2447909485,
  'positionSide': 'BOTH',
  'price': '3306.70',
  'qty': '0.010',
  'quoteQty': '33.06700',
  'realizedPnl': '0',
  'side': 'SELL',
  'symbol': 'MKRUSDT',
  'time': 1628301912771},
 {'buyer': True,
  'commission': '0.01319760',
  'commissionAsset': 'USDT',
  'id': 52525468,
  'maker': False,
  'marginAsset': 'USDT',
  'orderId': 2447502727,
  'positionSide': 'BOTH',
  'price': '3299.40',
  'qty': '0.010',
  'quoteQty': '32.99400',
  'realizedPnl': '-0.29700000',
  'side': 'BUY',
  'symbol': 'MKRUSDT',
  'time': 1628297857042},

现在,如何获得嵌套块的'time'值,其中'id'等于52532126?虽然我更喜欢矢量化,但我尝试遍历列表,但没有成功

for item in response_tradelist:
    if item == '52532126':
        print('ok')

即使它可以工作,我如何获得相应块中的“时间”值


Tags: idfalsetimebuyerpriceqtymakercommission
3条回答

由于您的id值在该字典块中是整数,因此无法使用字符串值进行检查, 您需要检查像item['id']==52532126这样的值

l=[{'buyer': False,
  'commission': '0.01333920',
  'commissionAsset': 'USDT',
  'id': 52549382,
  'maker': False,
  'marginAsset': 'USDT',
  'orderId': 2448678953,
  'positionSide': 'BOTH',
  'price': '3334.80',
  'qty': '0.010',
  'quoteQty': '33.34800',
  'realizedPnl': '0',
  'side': 'SELL',
  'symbol': 'MKRUSDT',
  'time': 1628311810877},
 {'buyer': True,
  'commission': '0.01334440',
  'commissionAsset': 'USDT',
  'id': 52544760,
  'maker': False,
  'marginAsset': 'USDT',
  'orderId': 2447909783,
  'positionSide': 'BOTH',
  'price': '3336.10',
  'qty': '0.010',
  'quoteQty': '33.36100',
  'realizedPnl': '-0.29400000',
  'side': 'BUY',
  'symbol': 'MKRUSDT',
  'time': 1628309268672},
 {'buyer': False,
  'commission': '0.01322680',
  'commissionAsset': 'USDT',
  'id': 52532126,
  'maker': False,
  'marginAsset': 'USDT',
  'orderId': 2447909485,
  'positionSide': 'BOTH',
  'price': '3306.70',
  'qty': '0.010',
  'quoteQty': '33.06700',
  'realizedPnl': '0',
  'side': 'SELL',
  'symbol': 'MKRUSDT',
  'time': 1628301912771},
 {'buyer': True,
  'commission': '0.01319760',
  'commissionAsset': 'USDT',
  'id': 52525468,
  'maker': False,
  'marginAsset': 'USDT',
  'orderId': 2447502727,
  'positionSide': 'BOTH',
  'price': '3299.40',
  'qty': '0.010',
  'quoteQty': '32.99400',
  'realizedPnl': '-0.29700000',
  'side': 'BUY',
  'symbol': 'MKRUSDT',
  'time': 1628297857042}]

for d in l:
    if d["id"]==52532126:
        print(d["time"])

如果你想把整本字典都印出来

 for d in l:
    if d["id"]==52532126:
        print(d)

这是一个字典列表,您必须与每个项目中的id进行比较,然后如果id匹配,则打印time

另外id值不是字符串,所以不要在id周围使用引号

试试这个:

for item in response_tradelist:
    if item['id'] == 52532126:
        print(item['time'])

如果要打印id为52532126的词典:

for dic in response_tradelist:
    if dic['id'] == 52532126:
        print(dic)

如果您特别需要时间,请将print(dic)替换为:

print(dic['time'])

相关问题 更多 >

    热门问题