Home > other >  Transform all floats ending in .0 in a dict to int
Transform all floats ending in .0 in a dict to int

Time:01-27

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])

CodePudding user response:

Recursive solution:

def convert_integer_floats_to_ints(d):
    for key, val in d.items():
        if isinstance(val, float) and val == int(val):                
            d[key] = int(val)
        elif isinstance(val, dict):
            convert_integer_floats_to_ints(val)

CodePudding user response:

the only solution i can think of:

for i in data.keys():
    if i!='time': data[i]['best_par']['n_neighbors'] = int(data[i]['best_par']['n_neighbors'])

Output:

{'4061_dummy': {'best_par': {'metric': 'minkowski',
   'n_neighbors': 6,
   'weights': 'distance'},
  'mae': 278.86288346930246},
 '4144_dummy': {'best_par': {'metric': 'l1',
   'n_neighbors': 4,
   'weights': 'uniform'},
  'mae': 116.66041666666666},
 '4181_fourier': {'best_par': {'metric': 'l1',
   'n_neighbors': 1,
   'weights': 'distance'},
  'mae': 604.05},
 '4331_fourier': {'best_par': {'metric': 'minkowski',
   'n_neighbors': 1,
   'weights': 'distance'},
  'mae': 305.13958333333335},
 '4555_fourier': {'best_par': {'metric': 'minkowski',
   'n_neighbors': 1,
   'weights': 'distance'},
  'mae': 888.1520833333333},
 '4583_fourier': {'best_par': {'metric': 'l1',
   'n_neighbors': 1,
   'weights': 'distance'},
  'mae': 601.28125},
 '4853_dummy': {'best_par': {'metric': 'minkowski',
   'n_neighbors': 2,
   'weights': 'distance'},
  'mae': 77.25801122256406},
 '4905_fourier': {'best_par': {'metric': 'minkowski',
   'n_neighbors': 24,
   'weights': 'uniform'},
  'mae': 610.8769965277778},
 'time': 1.47}

CodePudding user response:

It's quite straightforward.

for v in data.values():
    if isinstance(v,dict):
        v['best_par']['n_neighbors'] = int(v['best_par']['n_neighbors'])

This assumes you don't really need to troll through the deep structure LOOKING for such values. That's harder.

  •  Tags:  
  • Related