【问题标题】:Wrapping around lists with complex functions, iteration on numbered variables使用复杂函数环绕列表,迭代编号变量
【发布时间】:2014-01-15 02:50:33
【问题描述】:

我不是程序员,因此,我通过在这样的论坛上搜索来快速学习。 请耐心等待,因为我什至不使用本机 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 计算。由于上面列出的命名复杂性,我该如何环绕周围没有为列表的边界做一个单独的循环?这可能是一件小事,但我的数据集非常大,而且我正在进行大量计算。记住上面的例子是单个时间索引,但我有超过 150000 个时间索引和大约 150 个波形

【问题讨论】:

  • “我在这个论坛上发现了如何使用 global() 函数迭代变量。”在哪里?因为我真的去那里做什么并且对提出这个建议的人投反对票。不要不要使用globals()。将WF 变量放入一个列表并遍历该列表。除了“不起作用”之外,not 是否意味着 任何东西!如果它在 full 回溯后引发异常,如果它给出了您不期望的结果,则发布实际结果您所期望的!
  • @Bakuriu:我在精神上 200% 支持你,但至少有可能 OP 所在的环境将其数据结构注入到嵌入式 Python globals() 中。如果g["WF00"+str(1)] 有效,那么这可能就是正在发生的事情,尽管 OP 的更新似乎排除了这一点。不过,第一步可能仍然是从globals() 中提取它们并将它们放入一个列表或更有用的字典中。

标签: python list python-2.x


【解决方案1】:

你有关于为什么这行的任何错误输出

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() 函数,这可能是真的,也可能不是。无论如何..这些主要是猜测你的问题,一些错误输出会使这个答案更加集中。

【讨论】:

    【解决方案2】:

    是的,我在行中遇到的错误

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

    KeyError: 'WF001'
    

    当执行 print g.keys() 我得到这个列表。正如我所提到的,我不是程序员,这些 jey 对我来说意义不大。

    ['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)
    

    【讨论】:

      猜你喜欢
      • 2013-09-04
      • 2015-10-26
      • 1970-01-01
      • 1970-01-01
      • 2014-12-02
      • 2020-08-15
      • 2015-11-10
      • 2020-10-04
      • 1970-01-01
      相关资源
      最近更新 更多