-2

I am analyzing a series of molecular dynamic trajectories,

traj1
traj2 
traj3
...

I want to apply an analysis function to all them separately so that I get the different outputs also numbered

output1
output2
output3
...

Either that Or

traj1_output
traj2_output
traj3_output 

I was trying to write a loop, but couldnt really find the way.

Any suggestion would be highly appreciated.

Thanks a lot!

Kendas
  • 1,963
  • 13
  • 20
Rachael
  • 285
  • 3
  • 17
  • 3
    Can you perhaps show the loop you tried to write? – Kendas Aug 24 '17 at 12:31
  • Hi, yes sure, I was trying somthing like this, for i in range(len(traj_list)): topology_%d = traj_list[i].top % (i) Not sure if this makes sense I have very little Python experience. Thanks! – Rachael Aug 29 '17 at 15:22

2 Answers2

1

You can write function for one element and then use map() built-in function. Linc to documetation https://docs.python.org/2/library/functions.html#map

puf
  • 359
  • 3
  • 13
0

It seems to me that what you're trying to do is map inputs to outputs. While you can create variables on the fly with programmatically created names (see this answer), it is perhaps clearer to just use a dict:

def process(data):
    return "processed: {data}".format(data=data)

trajectories = {
    "traj1": "some data",
    "traj2": "some data",
    "traj3": "some data",
}

results = {}
for name in trajectories:
    output_name = "{name}_output".format(name=name)
    results[output_name] = process(trajectories[name])

# Print out all results (in semi-random order)
for name in results:
    print("{name}: {result}".format(name=name, result=results[name]))

# Print out a result from the dict
print(results["traj1_output"])

Note that the stub functions and values are there just so you can play around with the concept without the need for some complicated algorithm in your real program.

Kendas
  • 1,963
  • 13
  • 20