Importing number into variable name in python

First of all, i am somewhat new to python coding, so this might seem like a stupid question.

Problem: I am trying to create a script that allows me to import a number into (variable?operator?) names, in order to run my python script from a bash script.

Ideally i wanted to do the following (i know the syntax is wrong, but it is from my first try and captures what i would want it to do):

replica_number = 2 #2 is only for testing. It will later be exchanged with an imported number from a bash script over many different data sheets.

t_r+replica number = md.load(data_path+'potein_+replica_number+_centered.xtc', top = data_path+'potein_+replica_number+_first.gro')[1:]

What i want this to do is to automatically create the variables named t_r2 and import the files called protein_2_centered.xtc and protein_2_first.gro. However when i do this i get: SyntaxError: can’t assign to operator

Does anyone know how to get around this problem, or do i just have to make a separate script for every replica?


What you need is either a list or a dictionary.

you can keep all your results in a list (without keeping the replica_number):

t_r_list = []
t_r_list.append(md.load(...)[1:]) #  Run this line for each t_r you want to load

or if you want to keep the replica_number, you can use a dict:

t_r_dict = {}
t_r_dict[replica_number] = md.load(...)[1:]

You might want to read a tutorial on these data structures and how to use them, it will greatly help you on your journey with python later on and is the basis of the basis when it comes to working with data in python.