繁体   English   中英

从 dict python 列表中删除键

[英]Eliminate keys from list of dict python

我正在从这个网站 API 中提取信息: https : //financialmodelingprep.com/

具体来说,我需要损益表中的数据:

https://financialmodelingprep.com/developer/docs/#Company-Financial-Statements

我从 API 中得到的是一个列表,其中包含 36 个字典,其中包含以下数据:

[ {
  "date" : "2019-09-28",
  "symbol" : "AAPL",
  "fillingDate" : "2019-10-31 00:00:00",
  "acceptedDate" : "2019-10-30 18:12:36",
  "period" : "FY",
  "revenue" : 260174000000,
  "costOfRevenue" : 161782000000,
  "grossProfit" : 98392000000,
  "grossProfitRatio" : 0.378178,
  "researchAndDevelopmentExpenses" : 16217000000,
  "generalAndAdministrativeExpenses" : 18245000000,
  "sellingAndMarketingExpenses" : 0.0,
  "otherExpenses" : 1807000000,
  "operatingExpenses" : 34462000000,
  "costAndExpenses" : 196244000000,
  "interestExpense" : 3576000000,
  "depreciationAndAmortization" : 12547000000,
  "ebitda" : 81860000000,
  "ebitdaratio" : 0.314636,
  "operatingIncome" : 63930000000,
  "operatingIncomeRatio" : 0.24572,
  "totalOtherIncomeExpensesNet" : 422000000,
  "incomeBeforeTax" : 65737000000,
  "incomeBeforeTaxRatio" : 0.252666,
  "incomeTaxExpense" : 10481000000,
  "netIncome" : 55256000000,
  "netIncomeRatio" : 0.212381,
  "eps" : 2.97145,
  "epsdiluted" : 2.97145,
  "weightedAverageShsOut" : 18595652000,
  "weightedAverageShsOutDil" : 18595652000,
  "link" : "https://www.sec.gov/Archives/edgar/data/320193/000032019319000119/0000320193-19-000119-index.html",
  "finalLink" : "https://www.sec.gov/Archives/edgar/data/320193/000032019319000119/a10-k20199282019.htm"
}, ...
]

我在字典中不需要的是键:fillingDate、acceptedDate、link、finalLink

我设法删除了它们,但我的问题是,现在我写的那段代码过于频繁地吐出这些字典,我不明白为什么......

这是我尝试过的:

import requests
import json

url = "https://financialmodelingprep.com/api/v3/income-statement/AAPL?apikey=b60bb3d1967bb15bfb9daaa4426e77dc"
response = requests.get(url)
data = response.text
dataList = json.loads(data)
entriesToRemove = {
    'fillingDate' : 0,
    'acceptedDate' : 0,
    'link' : 0,
    'finalLink' : 0
}
removedEntries = []
newDict = {}

for index in range(len(dataList)):
    for key in dataList[index]:
        newDict[key] = dataList[index].get(key)                 
        if key in entriesToRemove:                              
            removedEntries = newDict.pop(key)                   
        print(json.dumps(newDict, indent=4))

提前致谢

操作:

对于字典中的每个键,字典都会打印一个新的时间。

原因:

for index in range(len(dataList)):
for key in dataList[index]:
    newDict[key] = dataList[index].get(key)                 
    if key in entriesToRemove:                              
        removedEntries = newDict.pop(key)                   
    print(json.dumps(newDict, indent=4))    # notice this line

为每个键打印字典的原因是因为对于字典上的每个key-val迭代,循环内都有一个print(json.dumps(newDict, indent=4))语句。

要从 dict 列表中删除突出显示的键,您可以遍历列表并创建另一个没有不必要键的 dict 列表:

s = [ {
  "date" : "2019-09-28",
  "symbol" : "AAPL",
  "fillingDate" : "2019-10-31 00:00:00",
  "acceptedDate" : "2019-10-30 18:12:36",
  "period" : "FY",
  "revenue" : 260174000000,
  "costOfRevenue" : 161782000000,
  "grossProfit" : 98392000000,
  "grossProfitRatio" : 0.378178,
  "researchAndDevelopmentExpenses" : 16217000000,
  "generalAndAdministrativeExpenses" : 18245000000,
  "sellingAndMarketingExpenses" : 0.0,
  "otherExpenses" : 1807000000,
  "operatingExpenses" : 34462000000,
  "costAndExpenses" : 196244000000,
  "interestExpense" : 3576000000,
  "depreciationAndAmortization" : 12547000000,
  "ebitda" : 81860000000,
  "ebitdaratio" : 0.314636,
  "operatingIncome" : 63930000000,
  "operatingIncomeRatio" : 0.24572,
  "totalOtherIncomeExpensesNet" : 422000000,
  "incomeBeforeTax" : 65737000000,
  "incomeBeforeTaxRatio" : 0.252666,
  "incomeTaxExpense" : 10481000000,
  "netIncome" : 55256000000,
  "netIncomeRatio" : 0.212381,
  "eps" : 2.97145,
  "epsdiluted" : 2.97145,
  "weightedAverageShsOut" : 18595652000,
  "weightedAverageShsOutDil" : 18595652000,
  "link" : "https://www.sec.gov/Archives/edgar/data/320193/000032019319000119/0000320193-19-000119-index.html",
  "finalLink" : "https://www.sec.gov/Archives/edgar/data/320193/000032019319000119/a10-k20199282019.htm"
}
]


res = []
ignored_keys = ['fillingDate', 'acceptedDate', 'link', 'finalLink']
for dd in s:
    for k,v in dd.items():
        if k not in ignored_keys:
            res.append({k: v})      
print(res)

编辑

单线:

print({k:v for dd in s for k,v in dd.items() if k not in ignored_keys})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM