python中的变量

2024-05-16 22:50:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到的变量如下:

ocd[2].likelihood.range[1][0]=[-5219, -5191, 11.7];
ocd[2].likelihood.range[1][1]=[-5180, -5057, 56.5];

从其他程序。我重新格式化为:

^{pr2}$

为了在Python中使用它们,但这是一项大量的工作,所以我的问题是,在Python中是否可以使用这些变量的原始名称?在


Tags: 程序名称rangeocdlikelihoodpr2
2条回答

在程序中,我不确定你会怎么做

  1. 在第一次使用range*变量之前添加以下代码:

    class Likelihood():
        def __init__(self, x, y):
            self.range = [{} for i in range(x)] # without default values
            # if you need default values, use this code or numpy.zeros(shape=(x,y))
            #self.range = [[0 for j in range(y)] for i in range(x)]
    
    class MyDataType():
        def __init__(self, dimension_1, dimension_2):
            self.likelihood = Likelihood(dimension_1, dimension_2)
    
    ocd = [0, 1, MyDataType(100, 100)]
    
    # copy-paste your code here:
    ocd[2].likelihood.range[1][0]=[-5219, -5191, 11.7];
    ocd[2].likelihood.range[1][1]=[-5180, -5057, 56.5];
    
    print(ocd[2].likelihood.range[1][0])
    
  2. 将所有range10替换为ocd[2].likelihood.range[1][0],例如,在记事本++Regex replace中:

    Find what:    range(\d)(\d)
    Replace with: ocd[2].likelihood.range[\1][\2]
    
    i.e. from code: print(range10)
    to code:        print(ocd[2].likelihood.range[1][0])
    

你可以用Python的方式来做:

def range_function(row, column):
    return ocd[2].likelihood.range[row][column]

range_function(1, 0) == [-5219, -5191, 11.7]

相关问题 更多 >