1

Yes, I have been informed 'bad idea', and I'd be happy to use another solution if what had been said actually seemed to do what I want it to.

Wanting to create a bunch of variables for genome analysis, but it seems dumb to do it multiple times. I want to do something like this:

aa = expr

ac = expr

ag = expr

at = expr

ca = expr

.

.

.

tt = expr

Where each expression is similar in nature but uses their respective letters (i.e cg is using c and g) as variables. I'm needing these to do further analysis as variables in more equations and to print them as well.

From a project I've just done in C, I'd set it up as two loops, one nested inside the other, linking to an array holding (a c g t) and use pointers... or something like that.

If someone could lead me down the right path, that'd be great. Everyone has been shouting "dictionaries!", but I'm not sure that's applicable here. If it is, I'm not really understanding how to use them properly.

  • Can you say more about expression i.e. is it some function of the two letters such as f('a', 'a') , f('a', 'c'), etc. – DarrylG May 13 '20 at 13:24
  • I'm writing a function to compare the amount of each nucleotide with a subsequent other in covid. ac being an 'a' followed by a 'c'. The input is a string, and the output should in the first equation be number of instances in the string. Further comparisons between these figures will give the enrichment of ca as compared to what would be expected from chance. – Liam Pouncy May 13 '20 at 13:53

2 Answers2

6

The preferred way is to use dictionaries - where the keys are strings, and therefore data. the content of a dictionary data["ac"] = expr can be accessed exactly as if it where a single variable, but the string "ac" can itself be in a variable (or be any expression).

It is feasible to create a "real" variable with a dynamic name by using Python's advanced introspection techniques, but then you'd have to use the same exquisite techniques to check if the variable exists, and again to use its contents.

Trust what is said about this: you want to use dictionaries.

data = {}   # empty dict

for firstletter in "acgt":
   for secondletter in "acgt":
      data[firstletter + secondletter] = expr


jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • I think this answers it pretty well, cheers. It's been pretty confusing switching into python from c, and you've better explained it for my case than other posts did. If I wanted to use each aa ca etc in a separate level, could I use them to create another dictionary with each of them as variables, and name them accordingly? – Liam Pouncy May 13 '20 at 13:47
  • Yes. I did not fully understand what you mean by "separate level", but you can eiher use the keys from a list, or other sequences, or you can create nested dictionaries with further data and use `data["ac"]["ga"]` to get to the nested value. And passing the whole dictionary as a parameter just pass a reference to the object, so it does not take more resources than using separate variables for partial data. – jsbueno May 13 '20 at 13:51
2

While it is indeed possible to programmatically name variables, it's considered a very bad idea unless you really absolutely need this feature. You don't.

The people shouting "dictionaries" are suggesting you create a dictionary, expressions = dict(). Now you can look up entries in expressions by strings, for example expressions["aa"] = 1.0, and so forth.

gspr
  • 11,144
  • 3
  • 41
  • 74