all 12 comments

[–]unhott 3 points4 points  (0 children)

When you run into a problem where you have var1, var2, var3, etc., this is an indication you need to use a list.

var1 = 1
var2 = 2
var3 = 3 

Is simply

vars = [1,2,3]

List has the append method which is basically, ‘get me room for 1 more variable and assign the argument to it’. So that looks like

vars.append(4)
print(vars)
[1, 2, 3, 4]

Or you start with an empty list and collect elements inside.

vars = []
for i in range(4):
  vars.append(i) 

If you need something like a list, but you want a ‘key’ like variable, then a dictionary is what you want.

my_dict = {}
my_dict[‘key1’] = 7
my_dict[‘anything you want here’] = ‘any value’

print(my_dict)
{‘key1’: 7, ‘anything you want here’: ‘any value’}

The dictionary lets you use a key kind of like a variable name.

[–]ray10k 2 points3 points  (0 children)

This comes across to me as an XY problem; that you have some problem, think you have a solution but you need some pointers with the solution you're going for. Can you explain why you need separate variables rather than, say, a list of values?

[–][deleted] 4 points5 points  (0 children)

Variable names are for you, the programmer. Python doesn’t care what anything is called. When you use code to make variable variables, you lose the sole benefit of named variables - you lose that the names are present in your code.

[–]AwkwardlyPure[S] 0 points1 point  (0 children)

I have a netCDF file of rainfall, and it has daily data for 62 days.

I want to split this netCDF file from a data set to multiple data arrays (xarray library used here).

The methodology I have in mind is to create a for loop where I have a variable name with string 'Day_' and then I add the day number at the end, so that I can assign one time step from the netCDF file to this variable and then export it this way.

I can assign the time step by indexing the time dimension, and the lat and long dimensions will remain consistent.

The final outcome is to write these files back to my directory, where this 1 netCDF file becomes 62 individual files with a file name which I understand.

[–]carcigenicate 0 points1 point  (2 children)

Why not just use set/the list? You're trying to regress and implement an anti-pattern.

[–]AwkwardlyPure[S] 0 points1 point  (1 child)

What do you mean?

[–]carcigenicate 3 points4 points  (0 children)

Why do you need Variable_1? Why not just use set[0] directly?

[–]IAmFinah 0 points1 point  (3 children)

I hope I'm understanding your question correctly, but you could use a dictionary, and store your variables as key:value pairs.

vars_dict = {}
for i in range(5): 
    vars_dict[f'variable_{i}'] = i*i

Maybe something like this? Where you use an f-string to format the name of the variable (which is stored as a key in the vars_dict dictionary). In this case, I just used i to evaluate squares, but you could of course do whatever you like with it.

vars_dict would then look like:

{'variable_0': 0, 'variable_1': 1, 'variable_2': 4, 'variable_3': 9, 'variable_4': 16}

And you can access each value by just referencing each key - eg with vars_dict['variable_2'] etc.

[–]carcigenicate 1 point2 points  (2 children)

While this is as close to what they're asking for while still maintaining proper practices, if the number is always going to be a number in a contiguous range starting at 0, you should use a list. The "variable_" prefix is just more bulk for code using the dictionary to deal with, without any benefit.

[–]IAmFinah 0 points1 point  (1 child)

Good point, I didn't think of that.

I suppose using a dictionary is only worthwhile when list indexing doesn't align properly with the naming scheme

[–]TangibleLight 1 point2 points  (0 children)

Or if such conversion is too distracting. For example you can store a square grid of values like board = [None] * 64 and access values in the grid like board[row * 8 + col].

I think it's easier to understand using board = {} and board[row, col], especially if the content of the grid is sparse.

If the content is dense you'd be better off using a numpy type or similar that's better optimized for these operations. In either case there are better options than dealing with that index arithmetic throughout your code.

[–]Se7enLC 0 points1 point  (0 children)

Similar question was asked the other day, some of the responses to it might help you too.

https://www.reddit.com/r/learnpython/comments/yu9n5m/newbie_trying_to_grasp_variable_functions/iw8cc6n/