【发布时间】:2020-04-28 07:52:06
【问题描述】:
当循环遍历ticker_list时,我有这个问题,而不是每个嵌套的dict[list]都得到它自己的值,它们都得到相同的值。
任何帮助将不胜感激。 干杯
ticker_list = ("AUD_USD","EUR_USD","USD_JPY","NZD_USD","EUR_JPY","GBP_USD","AUD_JPY")
granularity = "M15"
Defaults = {"bias":"x",
"Top_times":[0,0],
'Active_prices': [0, 0]}
active_dict = dict.fromkeys(ticker_list, Defaults)
pp.pprint(active_dict)
def active_range(ticker_list,direction,ref,active_dict):
try:
for ticker in ticker_list:
print(ticker)
### these 2 values below will be populated from pandas calculations hidden
### for simplification, i have used simple assignments to help show problem
toptime = ticker+"time"
long_t_price = "23"+ticker
active_dict = upkeep_dict(active_dict,ticker,"Top_times",toptime)
active_dict = upkeep_dict(active_dict,ticker,"Active_prices",long_t_price)
return active_dict
#exception removed for simplification
def upkeep_dict(active_dict_temp,ticker,path,new_item,list_limit=2):
## Check length of list is not > list_limit ###
if len(active_dict_temp[ticker][path])>(list_limit-1):
active_dict_temp[ticker][path].pop(0)
active_dict_temp[ticker][path].append(new_item)
else:
active_dict_temp[ticker][path].append(new_item)
return active_dict_temp
当我输入这个
fff = active_range(ticker_list,"Long",0,active_dict)
我买这个是为了 fff
{'AUD_USD': {'bias': 'x',
'Top_times': ['GBP_USDtime', 'AUD_JPYtime'],
'Active_prices': ['23GBP_USD', '23AUD_JPY']},
'EUR_USD': {'bias': 'x',
'Top_times': ['GBP_USDtime', 'AUD_JPYtime'],
'Active_prices': ['23GBP_USD', '23AUD_JPY']},
'USD_JPY': {'bias': 'x',
而不是这个
{'AUD_USD': {'bias': 'x',
'Top_times': ['0', 'AUD_USDtime'],
'Active_prices': [0, '23AUD_USD']},
'EUR_USD': {'bias': 'x',
'Top_times': ['0', 'EUR_USDtime'],
'Active_prices': ['0', '23EUR_USD']},
'USD_JPY': {'bias': 'x',
我做错了什么?
【问题讨论】:
标签: python-3.x loops dictionary