Python 如何在嵌套字典中迭代给定键的范围
我正在遍历一个嵌套的股票价格字典,它的关键字是:日期 我很难只得到开始日期和结束日期的股票价格 这是到嵌套字典的链接。 我尝试了很多不同的方法,但在过去的2小时内都无法解决:Python 如何在嵌套字典中迭代给定键的范围,python,Python,我正在遍历一个嵌套的股票价格字典,它的关键字是:日期 我很难只得到开始日期和结束日期的股票价格 这是到嵌套字典的链接。 我尝试了很多不同的方法,但在过去的2小时内都无法解决: starting_date = '2020-02-13' ending_date = '2020-02-11' #data = the nested dictionary opening_prices = [] series = data['Time Series (Daily)'] #Now were given
starting_date = '2020-02-13'
ending_date = '2020-02-11'
#data = the nested dictionary
opening_prices = []
series = data['Time Series (Daily)'] #Now were given the nested dictionary of the keys being dates and value being a dictionary of info: price.
for date in series:
if date == starting_date:
for data, price in date.items()
opening_prices.append(float(price['1. open']))
#we are retrieving the opening prices of the nested dict.
#For this attempt, I get the error of date being str type.
#I still don't know how to stop at the end_date as I tried a while loop, but it failed.
如果您的目标是只获取两个日期的值,您可以这样做:
>>> res = [float(series[k]['1. open']) for k in (starting_date, ending_date)]
>>> res
[741.84, 768.79]
对于一系列日期,可以将其更改为
>>> starting_date = '2020-02-11'
>>> ending_date = '2020-02-13'
>>> res = [float(series[k]['1. open']) for k in series if starting_date <= k <= ending_date]
>>> res
[741.84, 777.87, 768.79]
如果您的目标是只获取两个日期的值,您可以这样做:
>>> res = [float(series[k]['1. open']) for k in (starting_date, ending_date)]
>>> res
[741.84, 768.79]
对于一系列日期,可以将其更改为
>>> starting_date = '2020-02-11'
>>> ending_date = '2020-02-13'
>>> res = [float(series[k]['1. open']) for k in series if starting_date <= k <= ending_date]
>>> res
[741.84, 777.87, 768.79]
代码的问题是您试图从字符串访问密钥
data = {
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "TSLA",
"3. Last Refreshed": "2020-02-14",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2020-02-14": {
"1. open": "787.2200",
"2. high": "812.9700",
"3. low": "785.5000",
"4. close": "800.0300",
"5. volume": "15631446"
},
"2020-02-13": {
"1. open": "741.8400",
"2. high": "818.0000",
"3. low": "735.0000",
"4. close": "804.0000",
"5. volume": "26289348"
},
"2020-02-12": {
"1. open": "777.8700",
"2. high": "789.7500",
"3. low": "763.3700",
"4. close": "767.2900",
"5. volume": "12022470"
}
}
}
starting_date = '2020-02-12'
ending_date = '2020-02-14'
opening_prices = []
closing_prices = []
for date in data['Time Series (Daily)']:
if (date == starting_date):
opening_prices.append(float(data['Time Series (Daily)'][date]['1. open']))
if (date == ending_date):
closing_prices.append(float(data['Time Series (Daily)'][date]['4. close']))
print(opening_prices)
print(closing_prices)
这将产生:
[777.87]
[800.03]
[804.0]
所有日期介于
代码的问题是您试图从字符串访问密钥
data = {
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "TSLA",
"3. Last Refreshed": "2020-02-14",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2020-02-14": {
"1. open": "787.2200",
"2. high": "812.9700",
"3. low": "785.5000",
"4. close": "800.0300",
"5. volume": "15631446"
},
"2020-02-13": {
"1. open": "741.8400",
"2. high": "818.0000",
"3. low": "735.0000",
"4. close": "804.0000",
"5. volume": "26289348"
},
"2020-02-12": {
"1. open": "777.8700",
"2. high": "789.7500",
"3. low": "763.3700",
"4. close": "767.2900",
"5. volume": "12022470"
}
}
}
starting_date = '2020-02-12'
ending_date = '2020-02-14'
opening_prices = []
closing_prices = []
for date in data['Time Series (Daily)']:
if (date == starting_date):
opening_prices.append(float(data['Time Series (Daily)'][date]['1. open']))
if (date == ending_date):
closing_prices.append(float(data['Time Series (Daily)'][date]['4. close']))
print(opening_prices)
print(closing_prices)
这将产生:
[777.87]
[800.03]
[804.0]
所有日期介于
date==开始日期或date==结束日期不起作用?程序甚至无法识别开始日期。因此,对于date==start_date:它完全忽略开始日期,并将第一个开盘价附加到列表中,不管发生什么。在字典上迭代只通过它的键,即对于系列中的日期:意味着日期是一个字符串。用于系列中的日期、日期。项目:好的,我现在就试试,谢谢。我刚试过,但它仍然忽略了开始日期。日期==开始日期或日期==结束日期不起作用?程序甚至无法识别开始日期。因此,对于date==start_date:它完全忽略开始日期,并将第一个开盘价附加到列表中,不管发生什么。在字典上迭代只通过它的键,即对于系列中的日期:意味着日期是一个字符串。用于系列中的日期、日期。项目:好的,我现在就试试。谢谢。我刚试过,但它仍然忽略了开始日期。我的目标是在开始日期和结束日期之间追加开盘价的所有值。我现在正在玩DateTime模块。我的目标是在开始日期和结束日期之间附加开盘价的所有值。我现在正在玩DateTime模块。哦,我忘了添加正确的附加参数部分:['Time Series Daily'][date]['1.open']。我现在明白了。谢谢你说我很难只得到开始日期和结束日期的股票价格。对我来说,这意味着您只需要开始日期和结束日期。您还想要所有中间日期吗?当然,您可以以更优雅的方式转换日期。我把它留给你了:哦,我忘了添加正确的附加参数部分:['Time Series Daily'][date]['1.open']。我现在明白了。谢谢你说我很难只得到开始日期和结束日期的股票价格。对我来说,这意味着您只需要开始日期和结束日期。您还想要所有中间日期吗?当然,您可以以更优雅的方式转换日期。我把它留给你了:哦,天哪。你们是救生员。我尝试了很多不同的方法,但很长时间都有困难。我明白我做错了什么。非常感谢。我可以转移到我项目的下一部分。再次非常感谢!嗨,所以我用同样的概念来检索每个股票属性的数据。这个概念一直起作用,直到达到“体积”属性。出于某种原因,我收到了错误“KeyError:“Time Series Daily”。我想知道为什么我在过去的属性:打开、高、低、关闭时从未出现此错误时会出现此错误。错误发生在我分配:series=data['Time series Daily']时。所有的东西都打对了。我看不出为什么音量会和ohlc不一样。我需要看一下你的代码,找出你出错的原因。我可以把代码发给你吗?如果愿意,我们可以使用代码共享:哦,天哪。你们是救生员。我尝试了很多不同的方法,但很长时间都有困难。我明白我做错了什么。非常感谢。我可以转移到我项目的下一部分。再次非常感谢!嗨,所以我用同样的概念来检索每个股票属性的数据。这个概念一直起作用,直到达到“体积”属性。出于某种原因,我收到了错误“KeyError:“Time Series Daily”。我想知道为什么我在过去的属性:打开、高、低、关闭时从未出现此错误时会出现此错误。错误发生在我分配:series=data['Time series Daily']时。所有的东西都打对了。我看不出为什么音量会和ohlc不一样。我需要看一下你的代码,找出你出错的原因。我可以把代码发给你吗?如果愿意,我们可以使用代码共享: