我是一个非常新的导入数据,我试图使def按照所呈现的顺序读取。错误是在函数中,而不是在数据中。
def read_eos(filename: str, order: dict[str, int] = {"density": 0, "pressure": 1,
"energy_density": 2}):
# Paths to files
current_dir = os.getcwd()
INPUT_PATH = os.path.join(current_dir, "input")
in_eos = np.loadtxt(os.path.join(INPUT_PATH, filename))
eos = np.zeros(in_eos.shape)
# Density
eos[:, 0] = in_eos[:, order["density"]]
eos[:, 1] = in_eos[:, order["pressure"]]
eos[:, 2] = in_eos[:, order["energy_density"]]
return eos
看起来问题就在函数参数之一的类型提示中:dict[str, int]
。就Python而言,[str, int]
是dict
类型的下标,但dict
不能接受该下标,因此出现错误消息。
修复相当简单。首先,如果您还没有这样做,请在函数定义上方添加以下import语句:
from typing import Dict
然后,将dict[str, int]
改为Dict[str, int]
。