简体   繁体   English

遍历列表并将值分配给Python中的变量

[英]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. 我的coeffs []清单中有每个系数。 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? oo,我该怎么做? It would be like : a=coeff[0], b = coeff[1], ..., x = coeff[lvl] . 就像: 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. 如果您确实想为变量分配列表,则可以通过访问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. 然后,将在全局变量elm0elm1等中具有系数。

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? 因此,您只需将其等级的系数分配给索引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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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