How can I transform all float values in a dict ending in .0 to an int? Is there an elegant solution for this?
{
'4061_dummy': {
'mae': 278.86288346930246,
'best_par': {
'metric': 'minkowski',
'weights': 'distance',
'n_neighbors': 6.0
}
},
'4181_fourier': {
'mae': 604.05,
'best_par': {
'metric': 'l1',
'weights': 'distance',
'n_neighbors': 1.0
}
},
'4905_fourier': {
'mae': 610.8769965277778,
'best_par': {
'metric': 'minkowski',
'weights': 'uniform',
'n_neighbors': 24.0
}
},
'time': 1.47
}
My solution for this problem you can see in the following.
for i in a:
if isinstance(a[i], dict):
for key in a[i]["best_par"]:
if isinstance(a[i]["best_par"][key], float):
if int(a[i]["best_par"][key]) == a[i]["best_par"][key] and isinstance(a[i]["best_par"][key], float):
a[i]["best_par"][key] = int(a[i]["best_par"][key])
print(a[i]["best_par"][key])