2014-01-15 27 views
0

我不是程序員,因此我通過在這樣的論壇上搜索來即時學習。 忍受我,因爲我甚至沒有在本機Python上,但在一個應用程序有一個Python解釋器,允許代碼特定的處理用複雜的函數包裹列表,在編號變量上迭代

我見過我要問的例子,但他們不太類似我所需要的,所以我需要更具體地詢問

Q1:隨着時間的推移,我有一個包含波形(列表數組)的數據集。命名慣例是例如WF001WF002,...,WF009。我在這個論壇中發現如何用global()函數迭代變量。下面以列表(1,1)

g=globals() 
import ViewerStat as stat #this is a library from the application 

print g["WF00"+str(1)].value(1,1) #this command works fine. 

for i in range(1,9): 
     print g["WF00"+str(i)].value(1,1) # this do not work 

,我想最終值分配到由波形 下面的代碼在所有

for i in range (1,9) 
     g["NEW_WF00"+str(i)].value(1,1)=stat.median([g["WF00"+str(i)].value(1,1),g["WF00"+str(i+1)].value(1,1),g["WF00"+str(1+2)].value(1,1)]) 
不工作就只是第一個項目實例

注意:stat.median()功能在打印時不起作用,不會重複使用

print stat.median([g["WF00"+str(1)].value(1,1),g["WF00"+str(2)].value(1,1),g["WF00"+str(3)].value(1,1)]) 

所以我遍歷這個變量的問題,然後使用globals()進行數學函數後分配

Q2:列表的長度大約是300元。我將計算相鄰波形的中位數,並且我需要環繞列表,例如,元素300的中值將使用元素199,200和1進行計算。通過上面列出的命名併發症,我怎樣才能沒有爲列表的邊界做一個單獨的循環?這可能是一件小事,但我的數據集非常大,我正在做很多計算。記住上面的例子是一個單一的時間索引,但我有超過15萬的時間索引和大約150波形

+1

「我在這個論壇中發現如何使用global()函數迭代變量。」哪裏?因爲我*真的*要去那裏,並低估誰提出這個建議。 *不*使用'globals()'。將'WF'變量放入一個列表並遍歷列表。除「不起作用」之外*不*意指*任何*!如果它在* full *回溯後引發異常,如果它給出的結果是您不希望發佈實際結果*和*您期望的結果! – Bakuriu

+0

@Bakuriu:在精神上我和你有200%的差距,但OP至少有可能將其數據結構注入到嵌入式Python全局變量()中。如果'g [「WF00」+ str(1)]'有效,那麼這可能是發生了什麼,儘管OP的更新似乎排除了這一點。第一步仍然可能是將它們從'globals()'中提取出來,然後將它們放入一個列表或更有用的字典中。 – DSM

回答

0

你想過爲什麼這條線

print g["WF00"+str(i)].value(1,1) # this do not work 

不起作用任何錯誤輸出?

也許關於一個無效的密鑰?它看起來像「g」是一個如此前進的字典做出這個假設,我會嘗試打印出g.keys()來查看可用的鍵。一般來說遍歷一本字典,當我做:

for key in g.keys(): 
    g[key].value(1,1) 

這將確保只訪問鍵是實際上在那裏,你有什麼

for i in range(1,9): 
     print g["WF00"+str(i)].value(1,1) # this do not work 

可能未設置訪問元素。

上面我給

g[key].value(1,1) 

也使得假設的基礎上,你的代碼,該字典有有value()功能,這可能是也可能不是真正的目標線。無論如何..這些大多是猜測你的問題,一些錯誤輸出會使這個答案更集中。

0

是的,我在該行

print g["WF00"+str(i)].value(1,1) 

得到的錯誤是

KeyError: 'WF001' 

進行打印g.keys()時,我得到這個名單。正如我提到的,我不是程序員,而這些對我來說不算什麼。

['gt', 'help', 'SQRT2', 'random', 'imul', 'ge', 'TechlogMath', 'sqrt', 'acosh', '__package__', 'getDatasetZoneIndiceTop', 'ParameterProxy', 'LOG2E', 'le', 'getitem', 'Histogram', 'frexp', 'platform', 'add', 'delslice', 'degrees', 'outputTL', 'division', 'rshift', 'factorial', '__file__', 'findNearestIndexBefore', 'ceil', 'unitValueListConvert', 'iconcat', 'isCallable', 'mod', 'isub', '__group__', 'd', 'printError', 'radians', 'sin', 'setitem', 'SQRTH', 'fmod', 'output', 'div', 'LOOP', 'findNearestIndexUnsortedUncleanData', 'findNearestIndex', 'boxCox', 'not_', 'cosh', 'hypot', 'UF001', 'parameterDict', 'E', 'isMappingType', 'eq', 'asin', 'isnan', 'xor', 'sub', 'HistoryItem', 'MissingValue', 'sequenceIncludes', 'getslice', 'truth', 'LogView', 'indexOf', 'abs', 'exit', 'idiv', 'mul', 'PI', 'UN001', 'irepeat', 'is_', 'modf', '__builtins__', 'copysign', 'ws', 'sinh', 'ipow', 'LOOP_INV', 'trunc', 'or_', 'cos', 'CrossPlot', 'g', 'outputSHerr', 'CacheVarData', 'itruediv', 'ProgressBar', 'erfc', 'w', 'pow10', 'acos', 'LOOP_ARRAY_MVTEST', 'os', 'gamma', 'delitem', 'iand', 'countOf', 'pythonEditor', 'pow', 'fsum', 'pos', 'TechlogRealTime', 'ldexp', '__suffix__', 'and_', 'lshift', '__name__', 'ixor', '__prefix__', 'TechlogPlot', 'plot', 'LOOP_ARRAY', 'winId', 'log', 'invert', 'contains', 'lt', 'isSequenceType', 'irshift', 'Variable', 'PrintErrorLimit', 'limitValue', 'pi', 'log10', 'outputSH', 'exp10', 'asinh', 'expm1', 'LN2', 'iif', 'PIO2', 'LOOP_MVTEST', 'PIO4', 'TechlogStat', 'tanh', 'TechlogDatabase', '__doc__', 'lgamma', 'erf', 'getDatasetZoneIndiceBottom', 'isNumberType', 'ilshift', 'log1p', 'ifloordiv', 'almostEqual', 'outputLogger', 'inv', 'printErrorCount', 'LN10', 'tan', 'TechlogPlatform', 'iadd', 'LOOP_INV_ARRAY', 'fabs', 'floor', 'Parameter', 'atanh', 'ne', 'setslice', 'outputTLerr', 'attrgetter', 'ExitExpectedException', 'methodcaller', 'truediv', 'applyMode', '__techlog_database', 'LOOP_INV_MVTEST', 'boxCoxInv', 'unitValueConvert', 'stat', 'repeat', 'Serr', 'atan', 'index', 'LOG10E', 'db', 'ior', 'sys', 'printTest', 'isinf', 'Sout', 'is_not', 'concat', 'LOOP_INV_ARRAY_MVTEST', 'e', 'i', 'imod', 'atan2', 'floordiv', 'exp', 'neg', 'Array', 'itemgetter'] 

我應該如何實現你的例子:?

for key in g.keys(): 
    g[key].value(1,1)