简体   繁体   中英

Iterate through list and assign a value to the variable in Python

So i'm currently working on code, which solves simple differentials. For now my code looks something like that:

deff diff():
coeffs = []
#checking a rank of a function
lvl = int(raw_input("Tell me a rank of your function: "))
if lvl == 0:
    print "\nIf the rank is 0, a differential of a function will always be 0"
#Asking user to write coefficients (like 4x^2 - he writes 4)
for i in range(0, lvl):
    coeff = int(raw_input("Tell me a coefficient: "))
    coeffs.append(coeff)
#Printing all coefficients
print "\nSo your coefficients are: "
for item in coeffs:
    print item  

And so what I want to do next? I have every coefficient in my coeffs[] list. So now I want to take every single one from there and assign it to a different variable, just to make use of it. And how can I do it? I suppose I will have to use loop, but I tried to do so for hours - nothing helped me. Sooo, how can I do this? It would be like : a=coeff[0], b = coeff[1], ..., x = coeff[lvl] .

If you REALLY want to assign a list to variables you could do so by accessing the globals() dict. For example:

for j in len(coeffs):
     globals()["elm{0}".format(j)] = coeffs[j]

Then you'll have your coefficients in the global variables elm0 , elm1 and so on.

Please note that this is most probably not what you really want (but only what you asked for).

Just access the coefficients directly from the list via their indices.

If you are wanting to use the values in a different context that entails making changes to the values but you want to keep the original list unchanged then copy the list to a new list,

import copy

mutableCoeffs = copy.copy(coeffs)

You do not need new variables.

You already have all you need to compute the coefficients for the derivative function.

print "Coefficients for the derivative:"
l = len(coeffs) -1
for item in coeffs[:-1]:
    print l * item
    l -=1

Or if you want to put them in a new list :

deriv_coeffs = []
l = len(coeffs) -1
for item in coeffs[:-1]:
    deriv_coeffs.append(l * item)
    l -=1

I guess from there you want to differenciate no? So you just assign the cofficient times it rank to the index-1?

deff diff():

    coeffs = []
    #checking a rank of a function
    lvl = int(raw_input("Tell me a rank of your function: "))
    if lvl == 0:
        print "\nIf the rank is 0, a differential of a function will always be 0"
    #Asking user to write coefficients (like 4x^2 - he writes 4)
    for i in range(0, lvl):
        coeff = int(raw_input("Tell me a coefficient: "))
        coeffs.append(coeff)
    #Printing all coefficients
    print "\nSo your coefficients are: "
    for item in coeffs:
        print item  
    answer_coeff = [0]*(lvl-1)
    for i in range(0,lvl-1):
        answer_coeff[i] = coeff[i+1]*(i+1)
    print "The derivative is:"
    string_answer = "%d" % answer_coeff[0]
    for i in range(1,lvl-1):
        string_answer = string_answer + (" + %d * X^%d" % (answer_coeff[i], i))
    print string_answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM