0

I have 4 different algorithms each of which does different things on my data and they generate different outputs. I want to define only one piece of code instead of having four blocks of code for easier maintenance. However, I will have to name some variables dynamically. For example, if the algorithms names are A, B, C and D then, I should have 4 variables called output_A, output_B, output_C and output_D. How can I define this as something like output_{} which I can dynamically pass the algorithms names to it?

I know how to do this when the text is a number. For example, this code defines the varaible names as they have a postfix (01,02,03, ...) to their end.

predicted_file_format_UB = "UB_epoch{:02}.csv"

So, UB_epoch{:02}.csv will be UB_epoch01.csv, UB_epoch02.csv, UB_epoch03.csv etc. But I don't know how to do it when it's a text and the length is not constant. Different algotithms may have different name length.

HimanAB
  • 2,443
  • 8
  • 29
  • 43
  • 2
    the best answer is: don't. use a `dict`. – n1c9 Aug 30 '16 at 20:28
  • Is not possible to have a dictionary for all the output variables instead of defining separate variables? Then you can access that dictionary like this: `output["A"]`, `output["B"]`, `output["C"]`, ... – Vicente Olivert Riera Aug 30 '16 at 20:28
  • `algorithm = ['A', 'B', 'C', 'D']` `for any in algorithm:` `input = 'input_%s' % any` `output = 'output_%s' % any` `print(input, output)` – WeShall Aug 30 '16 at 20:34

0 Answers0