有没有一种方法可以迭代字典并在while循环中使用它



这就是我的代码在python中的样子。在df=get_data_df(id,start_at(的行中,我希望我的程序迭代id并在下面的程序中使用它,而不是逐个定义id。请帮助我如何迭代dictionary(id(并在while循环中使用它。

id= {'O': 6232,
'S': 5819,
'S': 5759,
'R': 6056,
'M': 6145,}
whole_df = pd.DataFrame()
start_at = int(datetime(2020,8,1,6,0,0,0, pytz.UTC).timestamp() * 1e6)
while True:
df = get_data_df(id,start_at)    
if df.shape[0] <= 1:
break
else:
whole_df = whole_df.append(df)
last_timestamp = whole_df.last_valid_index().timestamp()
start_at = int(last_timestamp * 1e6)
#print(whole_df)
for key in id:
print(key)

是一种你可以做到的方法。或者你可以这样做

i = 0
while True:
list(id)[i]
i += 1

只需提交一个指数并从该指数中获取每个点的值

有多种方法可以使用for循环迭代python字典:

for key in your_dict:
value = your_dict[key]
print(value)
for value in your_dict.values():
print(value)
for key, value in your_dict.items():
print(key, '=', value)

如果你真的想要一段时间的循环:

keys = your_dict.keys()
i = 0
while i < len(keys):
value = your_dict[keys[i]]
print(key, '=', value)

最新更新