2015-08-29 36 views
0
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 

目前結果看起來像這樣指數不能在Python工作

{'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]} 
+1

從你前面的問題之後,我覺得你有混合輸出和輸入。再次重新檢查它。 (請參閱鍵''x''的值) –

+0

我根據需要更改了輸入和代碼。 – TestEngineer

+0

除了'exp(6)'給出'e'功率6而不是'10'功率6.對於'10'功率6你需要'10 ** 6' –

回答

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]} 
+0

感謝您的幫助。雖然我無法弄清楚爲什麼我的GUI可以應用f(x)= x * 1E6,並且我無法在python中做同樣的事情。有了上面提到的代碼,至少我可以將指數值包裝爲'0'。 – TestEngineer

+0

是的,到處會有分歧!即使我遇到過這樣的情況。未來一切順利! :) –

0

exp( x)返回e的x次方。我認爲你需要10 ** x代替:

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