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

A couple questions about the way tuples work in Python

所以,我正在做麻省理工学院 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) 时,它returns 符合预期 - (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 的幂是 3,而它是 4。同样,为什么?

感谢您的宝贵时间!

给您带来麻烦的不是元组的行为,而是 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)

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

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)