Per Persson
Per Persson

Reputation: 185

variable in python

I get variables like these:

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

from an other program. I re-formate to:

range10=[-5219, -5191, 11.7];
range11=[-5180, -5057, 56.5];

for to use them in Python, but this is a lot of work, so my question is if it somehow is possible to use these variables with there original name in Python?

Upvotes: 0

Views: 155

Answers (2)

Aprillion
Aprillion

Reputation: 22304

not sure what you are asking,, but i'll give it a try - in the "other" program's code do the following:

  1. add this code before the first use of range* variables:

    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. replace all range10 to ocd[2].likelihood.range[1][0], e.g. in Notepad++ 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])
    

Upvotes: 2

iurisilvio
iurisilvio

Reputation: 4987

You can do it in a pythonic way:

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

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

Upvotes: 0

Related Questions