如何从具有多个项目的python词典中选择值

时间:2019-06-01 00:13:40

标签: python python-2.7

我正在尝试从python字典中获取值。如您在下面看到的,我有这个例子。我只想获取以"BTC_"开头的键,并在"BTC"值大于100时选择它。

d = { 
  'BTC_LTC':  { 'BTC': '138.13504038', 'LTC': '4662.34229096' }, 
  'BTC_MAID': { 'BTC': '10.38010322', 'MAID': '359919.71515255' },
  'BTC_NEM': { 'BTC': '30.34435435', 'NEM': '34435345' },
  'BTC_TRX': { 'BTC': '101.1122', 'TRX': '4546456456.343543543' },
  'USDC_BTC': { 'USDC': '481389.13175764', 'BTC': '74.73988488' },
  'USDC_ETH': { 'USDC': '72302.27016210', 'ETH': '357.72884034' },
  'totalBTC': '2340.96441558',
  'totalETH': '2771.63218462',
  'totalUSDC': '681255.56961992',
  'totalXMR': '267.83601213' 
}

预期结果如下:

['BTC_LTC', 'BTC_TRX'] 

由于其密钥以"BTC_"开头,并且其'BTC'的值大于100。

4 个答案:

答案 0 :(得分:0)

dic = { 
  BTC_LTC:  { BTC: '138.13504038', LTC: '4662.34229096' }, 
  BTC_MAID: { BTC: '10.38010322', MAID: '359919.71515255' },
  BTC_NEM: { BTC: '30.34435435', NEM: '34435345' },
  BTC_TRX: { BTC: '101.1122', TRX: '4546456456.343543543' },
  USDC_BTC: { USDC: '481389.13175764', BTC: '74.73988488' },
  USDC_ETH: { USDC: '72302.27016210', ETH: '357.72884034' },
  totalBTC: '2340.96441558',
  totalETH: '2771.63218462',
  totalUSDC: '681255.56961992',
  totalXMR: '267.83601213' 
}   

res = []
for i in dic:
    if isinstance(i,dict):
        if i['BTC']>100:
            res.append(i)

print(res)

答案 1 :(得分:0)

您可以尝试以下操作:

假设为给定词典分配一个变量

a = { 
  'BTC_LTC':  { 'BTC': '138.13504038', 'LTC': '4662.34229096' }, 
  'BTC_MAID': { 'BTC': '10.38010322', 'MAID': '359919.71515255' },
  'BTC_NEM': { 'BTC': '30.34435435', 'NEM': '34435345' },
  'BTC_TRX': { 'BTC': '101.1122', 'TRX': '4546456456.343543543' },
  'USDC_BTC': { 'USDC': '481389.13175764', 'BTC': '74.73988488' },
  'USDC_ETH': { 'USDC': '72302.27016210', 'ETH': '357.72884034' },
  'totalBTC': '2340.96441558',
  'totalETH': '2771.63218462',
  'totalUSDC': '681255.56961992',
  'totalXMR': '267.83601213' 
}   
result = []
for k, v in a.items():
   if k.startswith('BTC') and float(v.get('BTC', 0)) > 100:
       result.append(k)
print(result)

在Python中,字典中的键必须是不可变的,因此您的字典无效,必须将键转换为字符串或不可变的对象才能使用

https://docs.python.org/2.7/tutorial/datastructures.html#dictionaries

答案 2 :(得分:0)

假设键是字符串,则可以使用以下方法将字典分配给名为d的变量:

d = { 
  'BTC_LTC':  { 'BTC': '138.13504038', 'LTC': '4662.34229096' }, 
  'BTC_MAID': { 'BTC': '10.38010322', 'MAID': '359919.71515255' },
  'BTC_NEM': { 'BTC': '30.34435435', 'NEM': '34435345' },
  'BTC_TRX': { 'BTC': '101.1122', 'TRX': '4546456456.343543543' },
  'USDC_BTC': { 'USDC': '481389.13175764', 'BTC': '74.73988488' },
  'USDC_ETH': { 'USDC': '72302.27016210', 'ETH': '357.72884034' },
  'totalBTC': '2340.96441558',
  'totalETH': '2771.63218462',
  'totalUSDC': '681255.56961992',
  'totalXMR': '267.83601213' 
}   

>>> [k for k, v in d.items() if k.startswith('BTC_') and v.get('BTC', None) is not None and float(v.get('BTC')) > 100]

['BTC_LTC', 'BTC_TRX']

答案 3 :(得分:0)

使用列表理解功能,将键作为字符串:

inp = {
    'BTC_LTC':  { 'BTC': '138.13504038', 'LTC': '4662.34229096' },
    'BTC_MAID': { 'BTC': '10.38010322', 'MAID': '359919.71515255' },
    'BTC_NEM': { 'BTC': '30.34435435', 'NEM': '34435345' },
    'BTC_TRX': { 'BTC': '101.1122', 'TRX': '4546456456.343543543' },
    'USDC_BTC': { 'USDC': '481389.13175764', 'BTC': '74.73988488' },
    'USDC_ETH': { 'USDC': '72302.27016210', 'ETH': '357.72884034' },
    'totalBTC': '2340.96441558',
    'totalETH': '2771.63218462',
    'totalUSDC': '681255.56961992',
    'totalXMR': '267.83601213'
}

outp = [k for k, v in inp.items() if k[:4] == "BTC_" and float(v["BTC"]) > 100]

print(outp)