Python列表中的词典
我试图从字典列表中的字典中获取一些信息,我真的很挣扎。我正在尝试检查加密交换的余额,当我查询时,我收到以下词典列表:Python列表中的词典,python,list,dictionary,ccxt,Python,List,Dictionary,Ccxt,我试图从字典列表中的字典中获取一些信息,我真的很挣扎。我正在尝试检查加密交换的余额,当我查询时,我收到以下词典列表: [{'asset': 'BTC', 'free': '0.50000000', 'locked': '0.00000000'}, {'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'}, {'asset': 'ETH', 'free': '0.00000000', 'locked': '0.00000000
[{'asset': 'BTC', 'free': '0.50000000', 'locked': '0.00000000'},
{'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'ETH', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'NEO', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'BNB', 'free': '0.00000000']
我想做的就是能够选择资产
并返回免费
金额
我对字典没有太多的经验,但我希望能够做到以下几点:
if asset == 'BTC':
print(free)
在这种情况下,返回给我的结果是0.50000000
显然,我知道这行不通,但我不知道该怎么办
值得一提的是,我在python中使用了ccxt
Crypto包装器,使用fetch_balance()
,您可能认为会有一个参数,允许您选择要检查余额的货币,但似乎没有。如果有人知道其他情况,那也会很有帮助
非常感谢
for dictionary in list:
if dictionary['asset'] == 'BTC':
print(dictionary['free'])
它应该在列表中循环并打印每个
free
值,其中asset
等于“BTC”。首先注意,您编写的字典有误,需要用“}]”关闭它
这是一个适用于您的案例的代码,它在字典中迭代:
dict = [{'asset': 'BTC', 'free': '0.50000000', 'locked': '0.00000000'},
{'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'ETH', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'NEO', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'BNB', 'free': '0.00000000'}]
for element in dict:
if element['asset'] == 'BTC':
print(element['free'])
它打印0.5
import pandas as pd
list_of_dicts = [{'asset': 'BTC', 'free': '0.50000000', 'locked': '0.00000000'},
{'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'ETH', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'NEO', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'BNB', 'free': '0.00000000'}]
a = pd.DataFrame(list_of_dicts)
res = a.loc[a['asset'] == 'BTC']
res.iloc[0]["free"]
输出:
0.50000000
dict=[{'asset':'BTC','free':'0.50000000','locked':'0.00000000'},
{'asset':'LTC','free':'0.00000000','locked':'0.00000000'},
{'asset':'ETH','free':'0.00000000','locked':'0.00000000'},
{'asset':'NEO','free':'0.00000000','locked':'0.00000000'},
{'asset':'BNB','free':'0.00000000'}]
def printBTC(资产):
如果asset.asset='BTC':
打印(资产免费)
地图(打印BTC、dict)
工作的原因是map函数接受一个函数,并将数组的每个成员传递到函数中。例如,您可以将所有可用金额相加
In [1]: balance = [{'asset': 'BTC', 'free': '0.50000000', 'locked': '0.00000000'},
...: {'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'},
...: {'asset': 'ETH', 'free': '0.00000000', 'locked': '0.00000000'},
...: {'asset': 'NEO', 'free': '0.00000000', 'locked': '0.00000000'},
...: {'asset': 'BNB', 'free': '0.00000000'}]
In [2]: sum(float(a['free']) for a in balance)
Out[2]: 0.5
或者创建一个只包含资产名称和自由金额的新字典。这将更容易质疑
In [3]: info = {a['asset']: a['free'] for a in balance}
Out[3]:
{'BTC': '0.50000000',
'LTC': '0.00000000',
'ETH': '0.00000000',
'NEO': '0.00000000',
'BNB': '0.00000000'}
In [4]: info['BTC']
Out[4]: '0.50000000'
肾盂液: python的方法是使用内置函数:
next( x['free'] for x in your_list_of_dics if x['asset'] == 'BTC' )
示例:
l=[{'asset': 'BTC', 'free': '0.50000000', 'locked': '0.00000000'},
{'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'ETH', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'NEO', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'BNB', 'free': '0.00000000'}]
result = next( x['free'] for x in l if x['asset'] == 'BTC' )
print(result)
'0.50000000'
try:
l=[{'asset': 'BTC', 'free': '0.50000000', 'locked': '0.00000000'},
{'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'ETH', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'NEO', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'BNB', 'free': '0.00000000'}]
result = next( x['free'] for x in l if x['asset'] == 'BTC' )
print(result)
except StopIteration:
print ( "No 'BTC' value in any items." )
except KeyError::
print ( "No 'free' or no 'asset' fields in items." )
处理执行:
l=[{'asset': 'BTC', 'free': '0.50000000', 'locked': '0.00000000'},
{'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'ETH', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'NEO', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'BNB', 'free': '0.00000000'}]
result = next( x['free'] for x in l if x['asset'] == 'BTC' )
print(result)
'0.50000000'
try:
l=[{'asset': 'BTC', 'free': '0.50000000', 'locked': '0.00000000'},
{'asset': 'LTC', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'ETH', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'NEO', 'free': '0.00000000', 'locked': '0.00000000'},
{'asset': 'BNB', 'free': '0.00000000'}]
result = next( x['free'] for x in l if x['asset'] == 'BTC' )
print(result)
except StopIteration:
print ( "No 'BTC' value in any items." )
except KeyError::
print ( "No 'free' or no 'asset' fields in items." )
重要的是:如果任何项目的资产上有“BTC”值,会发生什么情况?如果一些dict没有“资产”或“免费”会发生什么情况?答案不错,但我认为对于初学者来说,这会使它变得复杂(仍然很好)。@Yagel-yep:)他们应该知道pd:)这正是我要找的。我实际上是在没有括号的情况下尝试的,因为我是个白痴。这是漫长的一天!谢谢