【问题标题】:how to get only specific values from dictionary using key in python如何使用python中的键从字典中仅获取特定值
【发布时间】:2020-06-10 11:16:53
【问题描述】:

我有这种 50 字典 意味着我有超过 50 种不同值的字典。比如symbol 和其他键值。

我只想在列表中获取 symbolopen 值。

{'pricebandupper': 380.65, 'symbol': 'ADANIPORTS', 'applicableMargin': 40.0, 'bcEndDate': '26-JUN-20', 'totalSellQuantity': None, 'adhocMargin': 18.19, 'companyName': 'Adani Ports and Special Economic Zone Limited', 'marketType': 'N', 'exDate': '17-JUN-20', 'bcStartDate': '19-JUN-20', 'css_status_desc': 'Listed', 'dayHigh': 349.75, 'basePrice': 346.05, 'securityVar': 18.31, 'pricebandlower': 311.45, 'sellQuantity5': None, 'sellQuantity4': None, 'sellQuantity3': None, 'cm_adj_high_dt': '10-JUN-19', 'sellQuantity2': None, 'dayLow': 340.4, 'sellQuantity1': None, 'quantityTraded': 2095819.0, 'pChange': 0.69, 'totalTradedValue': 8017.47, 'deliveryToTradedQuantity': 21.32, 'totalBuyQuantity': 2730.0, 'averagePrice': 344.84, 'indexVar': None, 'cm_ffm': 26717.33, 'purpose': 'ANNUAL GENERAL MEETING', 'buyPrice2': None, 'secDate': '10-Jun-2020 15:00:00', 'buyPrice1': 346.55, 'high52': 429.5, 'previousClose': 346.05, 'ndEndDate': None, 'low52': 203.0, 'buyPrice4': None, 'buyPrice3': None, 'recordDate': None, 'deliveryQuantity': 446896.0, 'buyPrice5': None, 'priceBand': 'No Band', 'extremeLossMargin': 3.5, 'cm_adj_low_dt': '23-MAR-20', 'varMargin': 18.31, 'sellPrice1': None, 'sellPrice2': None, 'totalTradedVolume': 2324983.0, 'sellPrice3': None, 'sellPrice4': None, 'sellPrice5': None, 'change': 2.4, 'surv_indicator': None, 'ndStartDate': None, 'buyQuantity4': None, 'isExDateFlag': False, 'buyQuantity3': None, 'buyQuantity2': None, 'buyQuantity1': 2730.0, 'series': 'EQ', 'faceValue': 2.0, 'buyQuantity5': None, 'closePrice': 0.0, 'open': 346.05, 'isinCode': 'INE742F01042', 'lastPrice': 348.45}

我尝试过的:

symbol = data['symbol']
open = data['open']

这里的数据是上面提到的字典。 请帮忙。 here 是几个字典的示例。

【问题讨论】:

  • 这能回答你的问题吗? Python dictionary get multiple values
  • 你能提供几个dicts的例子吗?你的字典放在列表中了吗?
  • 对于您尝试的事情,symbolopen 的预期值是多少,您得到的实际结果是什么?
  • @ОлегГребчук 抱歉回复晚了,我添加了字典示例。

标签: python python-3.x pandas list dictionary


【解决方案1】:

(我已经更新了这个答案,并假设字典存储在一个数组中)。

# So you a file with list having 50 dictionaries like this
[ {}, {}, {}, {}, {} ]

为了解决这个问题,我们只需遍历数组,得到一个索引,然后从字典列表的每个索引中获取值:

'''
Assuming the list goes like this
list_data = [{}, {}, {}, {}] having you dictionary only, I am just showing it 
for the sake of understanding
'''
list_data = [{}, {}, {} ....]
new_list = [] # where you want to store the result

# traversing through each dict item in list_data
# dictionary.get(key) is very famously used for getting exact key's value
# Read more about dictionaries here: 
# https://www.w3schools.com/python/python_dictionaries.asp
for i in range(len(list_data)):
    #assuming that every dictionary has the key 'symbol' and 'open'
    new_list.append([list_data[i].get('symbol'), list_data[i].get('open')])

print(new_list)

# Output will be consisting the array of array, like this
# [ ['ADANIPORTS', 346.05], .... ] since every dictionary should show up it's 
# symbol and open value uniquely
# ..... => This is for more values
>>> [['ADANIPORTS', 346.05], ..... ]

【讨论】:

  • 感谢您的回答。实际上它只打印'ADANIPORTS', 346.05,但我有50个问题中提到的这类字典。这是所有字典。pastebin.com/C8w077Nr
  • 所以你想遍历每一个字典,并打印出symbolprice的值?我是否满足您的要求@SandeepSharma?
  • 你能不能把那些字典存到[],如果是的话,尽快告诉我,我会给你解决办法。或将其存储到列表中或[] 并告诉我,我将给出解决方案然后@SandeepSharma
  • 是的,所以我假设您可以将 50 个字典存储在 lsit 中,我将给出相同的解决方案 @SandeepSharma
  • 是的。 like 字典仅包含 symbol 和其他 open
【解决方案2】:

你可以试试这个不确定你想要什么:

data = {'pricebandupper': 380.65, 'symbol': 'ADANIPORTS', 'applicableMargin': 40.0, 'bcEndDate': '26-JUN-20', 'totalSellQuantity': None, 'adhocMargin': 18.19, 'companyName': 'Adani Ports and Special Economic Zone Limited', 'marketType': 'N', 'exDate': '17-JUN-20', 'bcStartDate': '19-JUN-20', 'css_status_desc': 'Listed', 'dayHigh': 349.75, 'basePrice': 346.05, 'securityVar': 18.31, 'pricebandlower': 311.45, 'sellQuantity5': None, 'sellQuantity4': None, 'sellQuantity3': None, 'cm_adj_high_dt': '10-JUN-19', 'sellQuantity2': None, 'dayLow': 340.4, 'sellQuantity1': None, 'quantityTraded': 2095819.0, 'pChange': 0.69, 'totalTradedValue': 8017.47, 'deliveryToTradedQuantity': 21.32, 'totalBuyQuantity': 2730.0, 'averagePrice': 344.84, 'indexVar': None, 'cm_ffm': 26717.33, 'purpose': 'ANNUAL GENERAL MEETING', 'buyPrice2': None, 'secDate': '10-Jun-2020 15:00:00', 'buyPrice1': 346.55, 'high52': 429.5, 'previousClose': 346.05, 'ndEndDate': None, 'low52': 203.0, 'buyPrice4': None, 'buyPrice3': None, 'recordDate': None, 'deliveryQuantity': 446896.0, 'buyPrice5': None, 'priceBand': 'No Band', 'extremeLossMargin': 3.5, 'cm_adj_low_dt': '23-MAR-20', 'varMargin': 18.31, 'sellPrice1': None, 'sellPrice2': None, 'totalTradedVolume': 2324983.0, 'sellPrice3': None, 'sellPrice4': None, 'sellPrice5': None, 'change': 2.4, 'surv_indicator': None, 'ndStartDate': None, 'buyQuantity4': None, 'isExDateFlag': False, 'buyQuantity3': None, 'buyQuantity2': None, 'buyQuantity1': 2730.0, 'series': 'EQ', 'faceValue': 2.0, 'buyQuantity5': None, 'closePrice': 0.0, 'open': 346.05, 'isinCode': 'INE742F01042', 'lastPrice': 348.45}

result = []

result.append(data['symbol'])
result.append(data['open'])

print(result)

【讨论】:

    【解决方案3】:

    虽然您没有指定希望输出的确切格式。假设您希望列表中某些键的所有值,这里是您可以使用的示例代码。

    inp_dict = {1:2,3:4} keys = [1,3] output_lis = [a.get(i) for i in b]

    dictionary[key] 也被使用,但如果缺少键,'get' 内置函数允许您提供默认值。

    dictionary.get(key[, default])

    【讨论】:

      猜你喜欢
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 2022-12-05
      • 2020-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      相关资源
      最近更新 更多