Suppress Scientific Notation in Numpy When Creating Array From Nested List

前端 未结 4 1734
忘掉有多难
忘掉有多难 2020-11-30 17:48

I have a nested Python list that looks like the following:

my_list = [[3.74, 5162, 13683628846.64, 12783387559.86, 1.81],
 [9.55, 116, 189688622.37, 26033226         


        
4条回答
  •  清歌不尽
    2020-11-30 18:15

    You could write a function that converts a scientific notation to regular, something like

    def sc2std(x):
        s = str(x)
        if 'e' in s:
            num,ex = s.split('e')
            if '-' in num:
                negprefix = '-'
            else:
                negprefix = ''
            num = num.replace('-','')
            if '.' in num:
                dotlocation = num.index('.')
            else:
                dotlocation = len(num)
            newdotlocation = dotlocation + int(ex)
            num = num.replace('.','')
            if (newdotlocation < 1):
                return negprefix+'0.'+'0'*(-newdotlocation)+num
            if (newdotlocation > len(num)):
                return negprefix+ num + '0'*(newdotlocation - len(num))+'.0'
            return negprefix + num[:newdotlocation] + '.' + num[newdotlocation:]
        else:
            return s
    

提交回复
热议问题