[Tutor] I don’t know how to generate a number of data points from this part of code

Alex Kleider akleider at sonic.net
Thu Feb 27 12:55:56 EST 2020


On 2020-02-26 21:09, SATYABRATA DATTA wrote:
> I have a package and this part is written to calculate the quantity 
> called
> ‘action’ at a output temperature T. But now I need a set of ‘action’ at
> slightly different temperatures say T+1e-3,T+3*1e-3,T+4*1e-3… Etc.Since 
> I
> am a beginner at python I can’t figure out where in. This definition I 
> have
> to put a loop to print different values of action at slightly different
> temperatures. The part of code is attached as below

I don't pretend to understand (or to even have looked very hard) at the 
code
you submitted but based only on what you've written above:
Assuming you have a function that calculates something (you are calling
it 'action') based on a quantity, in your case it seems to be a 
temperature
_you_ can create a function:

def action_at_t(temp):
    ....
    return action

Once you have a collection of temperatures: collection_of_temperatures:

results = []  # results = set()
for temp in collection_of_temperatures:
     results.append(action_at_t(temp))  # use 'add' vs 'append' if using 
a set
for res in results:
     print(res)

If you know about list comprehension that would be even better.

You might be able to generate your collection_of_temperatures using the 
range function.

Comments are in case you really want a set rather than a list (which I 
doubt.)
A dict keyed by temperature would seem to me to be what would be best.


More information about the Tutor mailing list