繁体   English   中英

关于元组在Python中的工作方式的几个问题

[英]A couple questions about the way tuples work in Python

因此,我在MIT OCW的《计算机科学与程序设计导论》中做了这个问题:

Problem #2
Implement the compute_deriv function. This function computes the derivative 
of a polynomial function. It takes in a tuple of numbers poly and returns 
the derivative, which is also a polynomial represented by a tuple.

def compute_deriv(poly):

"""
Computes and returns the derivative of a polynomial function. If the

derivative is 0, returns (0.0,).

Example:

>>> poly = (-13.39, 0.0, 17.5, 3.0, 1.0)    # x4 + 3.0x3 + 17.5x2 - 13.39 

>>> print compute_deriv(poly)               # 4.0x3 + 9.0x2 + 35.0x 

(0.0, 35.0, 9.0, 4.0)

poly: tuple of numbers, length > 0

returns: tuple of numbers

"""

# TO DO ...

这是我的程序(有效):

def compute_deriv(poly):
    derivatives=()
    for i in poly:
        if list(poly).index(i)==0:     #my version is 2.5.4, so I cannot use 
            continue                      #tuple.index(i) and since I didn't 
        else:                             #find another way, I converted the 
            deriv=i*list(poly).index(i)           #tuple to a list
            derivatives=derivatives+(deriv,)
    return derivatives
polyx=(-13.39, 0.0, 17.5, 3.0, 1.0)
print compute_deriv(polyx)
polyxx=(1.3, 7.0, 4.0, 2.5, 0.0, -8.0)
print compute_deriv(polyxx)

首先,我想让程序要求我输入多项式,而不是在内部写入多项式:

...
polyx=tuple(raw_input("Enter your polynomial tuple here:"))
print compute_deriv(polyx)

但这不起作用:

Enter your tuple here:-13.39, 0.0, 17.5, 3.0, 1.0
('1', '33', '...', '33', '99999', ',,,,,,', '       ', '00000000', '...', 
'00000000', ',,,,,,', '       ', '1', '77777777777777', '...', 
'5555555555555555', ',,,,,,', '       ', '33', '...', '00000000', ',,,,,,', 
' ', '1', '...', '00000000')

为什么? 而另一个问题是第二个元组(-8x ^ 5 + 2.5x ^ 3 + 4x ^ 2 + 7x + 1.3)-当其成员分别为(1.3、7.0、4.0、2.5、0.0,-8.0)时返回预期的值-(7.0,8.0,7.5,0.0,-40.0),但是如果第一个成员为0.0(如-8x ^ 5 + 2.5x ^ 3 + 4x ^ 2 + 7x),则更改为-(7.0, 8.0、7.5,-40.0)。 省略了第二个0.0,这是一个问题,因为它暗示着-40.0的幂在为4时为3。同样,为什么呢?

感谢您的时间!

raw_input会将您的输入转换为字符串。 因此,输入-13.39, 0.0, 17.5, 3.0, 1.0将产生一个字符串。

然后,将其转换为元组。 默认情况下,给定字符串时, tuple会拆分字符串中的每个字符,并与所有字符组成一个元组。 例:

tuple("Hello World")

输出:( ('H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd')

这就是为什么您得到那些结果。 可能的解决方案:输入用逗号','分隔的元组,然后将其拆分:

polyx = tuple(raw_input("Enter your polynomial tuple here: ").split(","))

例:

Enter your polynomial tuple here: -13.39,0.0,17.5,3.0,1.0

输出: (u'-13.39', u'0.0', u'17.5', u'3.0', u'1.0')

但是请注意,该元组将包含字符串,而不是浮点数。 您需要转换它们:

polyx = tuple(float(x) for x in raw_input("Enter your polynomial tuple here: ").split(","))

例:

Enter your polynomial tuple here: -13.39, 0.0, 17.5, 3.0, 1.0

输出: (-13.39, 0.0, 17.5, 3.0, 1.0)

导致您麻烦的不是元组的行为,而是raw_input()的行为。

>>> polyx=tuple(raw_input("Enter your polynomial tuple here:"))
Enter your polynomial tuple here:-13.39, 0.0, 17.5, 3.0, 1.0
>>> polyx
('-', '1', '3', '.', '3', '9', ',', ' ', '0', '.', '0', ',', ' ', '1', '7', '.', '5', ',', ' ', '3', '.', '0', ',', ' ', '1', '.', '0')

可以改用input()

>>> polyx=tuple(input("Enter your polynomial tuple here:"))
Enter your polynomial tuple here:-13.39, 0.0, 17.5, 3.0, 1.0
>>> polyx
(-13.39, 0.0, 17.5, 3.0, 1.0)

但这被认为是不安全的(也许不是在这里,而是一般而言)。 另一种选择是自己解析输入行。

暂无
暂无

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

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