converting from string to float or integer depending on their nature in python

Viewed 132

After parsing a file, I obtain a list of strings containing numerical values, let's say:

my_list = ['1', '-2.356', '00.57', '0', '-1', '02678', '0.005367', '0', '1']

In order to obtain these numerical values, I do the following:

new_list = [float(i) for i in my_list] . 

The problem is that the integer values - which are a majority in the file I am handling, are also converted to float and thus occupy more memory - let alone other issues (I have to use some of them as indexes - thus they need to be converted to int at some point..)

Is there an efficient way to convert from string to float only those needed (I must not lose any precision) and to integer all the other ones?

2 Answers

You can built a function that converts the strings to integers if possible and floats otherwise (and if even that fails, leave them as strings).

my_list = ['1', '-2.356', '00.57', '0', '-1', '02678', '0.005367', '0', '1']

def converter(n):
  try:
    return int(n)
  except ValueError:
    try:
      return float(n)
    except ValueError:
      return n  # <- left as string

print([converter(x) for x in my_list])  # -> [1, -2.356, 0.57, 0, -1, 2678, 0.005367, 0, 1]

This works because int('2.3') is not the same as int(2.3). The first returns an Error while the second, clips the float and returns 2.

Also note that the order in which the try blocks are arranged is very important since float('2') does work. As a result, casting to int has to be tried first!

The difference between int and float in your list is whether or not there is a ., you can use that to choose what casting to use.

new_list = [float(elt) if '.' in elt else int(elt) for elt in my_list] 

edit:

In order to handle float special cases, the converter function proposed by @EvKounis is a good idea; expanding on it after @PM2Ring remark:

my_list = ['1', '1E3', '-inf', 'inf', 'NaN', 'nan', '-2.356', '00.57', '0', '-1', '02678', '0.005367', '0', '1', '398472398472943657410843104729572471308172374018301478744723974523987452938729847194719841471476574572394710481048075434810482398752481038185739847239847294365741084310472957247130817237401830147874472397452398745293872984719471984147147657457239471048104807543481048239875248103818573984723984729436574108431047295724713081723740183014787447239745239874529387298471947198414714765745723947104810480754348104823987524810381857398472398472943657410843104729572471308172374018301478744723974523987452938729847194719841471476574572394710481048075434810482398752481038185739847239847294365741084310472957247130817237401830147874472397452398745293872984719471984147147657457239471048104807543481048239875248103818573984723984729436574108431047295724713081723740183014787447239745239874529387298471947198414714765745723947104810480754348104823987524810381857398472398472943657410843104729572471308172374018301478744723974523987452938729847194719841471476574572394710481048075434810482398752481038185739847239847294365741084310472957247130817237401830147874472397452398745293872984719471984147147657457239471048104807543481048239875248103818573984723984729436574108431047295724713081723740183014787447239745239874529387298471947198414714765745723947104810480754348104823987524810381857398472398472943657410843104729572471308172374018301478744723974523987452938729847194719841471476574572394710481048075434810482398752481038185739847239847294365741084310472957247130817237401830147874472397452398745293872984719471984147147657457239471048104807543481048239875248103818573984723984729436574108431047295724713081723740183014787447239745239874529387298471947198414714765745723947104810480754348104823987524810381857']

def convert_to_int_or_float(elt):
    try:
        e = float(elt)
    except ValueError:
        raise ValueError
    elt = elt.lower()
    if '.' in elt or 'e' in elt or 'inf' in elt or 'nan' in elt:
        pass
    else:
        e = int(elt)
    return e

[convert_to_int_or_float(e) for e in my_list]

output:

[1,
 1000.0,
 -inf,
 inf,
 nan,
 nan,
 -2.356,
 0.57,
 0,
 -1,
 2678,
 0.005367,
 0,
 1,
 398472398472943657410843104729572471308172374018301478744723974523987452938729847194719841471476574572394710481048075434810482398752481038185739847239847294365741084310472957247130817237401830147874472397452398745293872984719471984147147657457239471048104807543481048239875248103818573984723984729436574108431047295724713081723740183014787447239745239874529387298471947198414714765745723947104810480754348104823987524810381857398472398472943657410843104729572471308172374018301478744723974523987452938729847194719841471476574572394710481048075434810482398752481038185739847239847294365741084310472957247130817237401830147874472397452398745293872984719471984147147657457239471048104807543481048239875248103818573984723984729436574108431047295724713081723740183014787447239745239874529387298471947198414714765745723947104810480754348104823987524810381857398472398472943657410843104729572471308172374018301478744723974523987452938729847194719841471476574572394710481048075434810482398752481038185739847239847294365741084310472957247130817237401830147874472397452398745293872984719471984147147657457239471048104807543481048239875248103818573984723984729436574108431047295724713081723740183014787447239745239874529387298471947198414714765745723947104810480754348104823987524810381857398472398472943657410843104729572471308172374018301478744723974523987452938729847194719841471476574572394710481048075434810482398752481038185739847239847294365741084310472957247130817237401830147874472397452398745293872984719471984147147657457239471048104807543481048239875248103818573984723984729436574108431047295724713081723740183014787447239745239874529387298471947198414714765745723947104810480754348104823987524810381857]
Related