我正在编写一个函数来将预测变量保存到 json 文件中。最初,该变量是 [1,0,1 ... 0, 0 1] 形式的 array.numpy16 类型。在函数代码中,我必须为数据添加一个 id。如何替换get参数或重写函数
import pandas
df = pandas.DataFrame(data=predictions)
def save_output(data, path):
with open(path, mode="w") as file:
for line in sorted(data, key=lambda x: int(x.get("idx"))):
line["idx"] = int(line["idx"])
file.write(f"{json.dumps(line, ensure_ascii=False)}\n")
1 def save_output(data, path):
2 with open(path, mode="w") as file:
----> 3 for line in sorted(data, key=lambda x: int(x.get("idx"))):
4 line["idx"] = int(line["idx"])
5 file.write(f"{json.dumps(line, ensure_ascii=False)}\n")
AttributeError: 'int' object has no attribute 'get'