指数不适用于Python

时间:2015-08-29 15:13:01

标签: python string dictionary

import math
from math import exp
dic_1 = {'x': [0.0005697499999999999, 0.00056825,0.00056825,0.00056825,0.00056825],'y': [-1.1e-05, 548],'z': [653,  672,150,590],'w': [653, 672,150,590]}
dic_1 = {key:[i*(exp(6)) for i in val] for key,val in dic_1.items()}
print dic_1

目前结果看起来像这样

{'y': [-0.00022094090615506435, 11006.874233906841], 'x': [0.011443734661986173, 0.011413606356601392, 0.011413606356601392, 0.011413606356601392, 0.011413606356601392], 'z': [13115.855610841547, 13497.480812382113, 3012.83053847815, 11850.466784680724], 'w': [13115.855610841547, 13497.480812382113, 3012.83053847815, 11850.466784680724]}

但我想要的是

{'y': [0, 548000000], 'x': [569.75, 568.25, 568.25, 568.25, 568.25], 'z': [653000000, 672000000, 150000000, 590000000], 'w': [653000000, 672000000, 150000000, 590000000]}`

如果我使用dic_1 = {key:[i*(10**3) for i in val] for key,val in dic_1.items()} 我得到以下结果

{'y': [-11.0, 548000000], 'x': [569.75, 568.25, 568.25, 568.25, 568.25], 'z': [653000000, 672000000, 150000000, 590000000], 'w': [653000000, 672000000, 150000000, 590000000]}

2 个答案:

答案 0 :(得分:0)

exp(x)将e返回到x幂。我认为你需要10 ** x:

...   key:[i*10**6 for i in val   ... 

答案 1 :(得分:0)

在评论中经过大量讨论后,我终于明白了你的问题。代码可以写成

dic_1 = {'x': [0.0005697499999999999, 0.00056825,0.00056825,0.00056825,0.00056825],'y': [-1.1e-05, 548],'z': [653,  672,150,590],'w': [653, 672,150,590]}

def tester(key,value):
    if 'e' in str(value):
        return 0
    #elif key in ('x'):     # This was what you wanted last time. 
    #    return value       # I have commented it out to match output
    else:
        return (10**6) * value

newdic = {key:[tester(key,i) for i in value] for key,value in dic_1.items()}
print newdic

输出符合预期

{'y': [0, 548000000], 'x': [569.75, 568.25, 568.25, 568.25, 568.25], 'z': [653000000, 672000000, 150000000, 590000000], 'w': [653000000, 672000000, 150000000, 590000000]}

或者我们也可以尝试这个,如果我们不想解析任何'x'值。

def tester(key,value):
    if ('e' in str(value)) & (key not in ('x')):
        return 0
    elif key in ('x'):     # This was what you wanted last time.
        return value       # I have commented it out to match output
    else:
        return (10**6) * value

newdic = {key:[tester(key,i) for i in value] for key,value in dic_1.items()}
print newdic

输出符合预期

{'y': [0, 548000000], 'x': [0.0005697499999999999, 0.00056825, 0.00056825, -1.1e-05, 0.00056825, -1.1e-05, -1.1e-05, 0.0005697499999999999, 0.00056825, 0.00056825, -1.1e-05], 'z': [0, 0, 150000000, 590000000], 'w': [653000000, 0, 150000000, 590000000]}