Py学习  »  Eric Duminil  »  全部回复
回复总数  2
9 年前
回复了 Eric Duminil 创建的主题 » 与python列表一起使用的and运算符[重复]

高查斯

是的,有一些问题。

fn() == fn(3) == fn(4, 4)

首先,如果 fn 收益率 0 您不能知道它是否被调用,没有任何参数,只有一个参数或多个相等的参数:

>>> fn()
0
>>> fn(3)
0
>>> fn(3, 3, 3)
0

什么? FN 意思是?

那么,python是一种动态语言。没有具体说明什么 FN 是的,它的输入应该是什么,输出应该是什么样子。因此,正确命名函数非常重要。同样,不必调用参数 args . delta(*numbers) 或 calculate_range(*numbers) 可以更好地描述函数应该做什么。

参数错误

最后,合乎逻辑的 and 运算符应该防止函数在没有任何参数的情况下调用时失败。如果某个参数不是数字,它仍然失败,尽管:

>>> fn('1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in fn
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>> fn(1, '2')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in fn
TypeError: '>' not supported between instances of 'str' and 'int'
>>> fn('a', 'b')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in fn
TypeError: unsupported operand type(s) for -: 'str' and 'str'

可能的替代方案

下面是根据 "Easier to ask for forgiveness than permission." principle :

def delta(*numbers):
    try:
        return max(numbers) - min(numbers)
    except TypeError:
        raise ValueError("delta should only be called with numerical arguments") from None
    except ValueError:
        raise ValueError("delta should be called with at least one numerical argument") from None

例如:

>>> delta()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in delta
ValueError: delta should be called with at least one numerical argument
>>> delta(3)
0
>>> delta('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in delta
ValueError: delta should only be called with numerical arguments
>>> delta('a', 'b')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in delta
ValueError: delta should only be called with numerical arguments
>>> delta('a', 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in delta
ValueError: delta should only be called with numerical arguments
>>> delta(3, 4.5)
1.5
>>> delta(3, 5, 7, 2)
5

如果你真的不想在 delta 在没有任何参数的情况下调用,您可以返回一些其他情况下无法返回的值(例如。 -1 或 None ):

>>> def delta(*numbers):
...     try:
...         return max(numbers) - min(numbers)
...     except TypeError:
...         raise ValueError("delta should only be called with numerical arguments") from None
...     except ValueError:
...         return -1 # or None
... 
>>> 
>>> delta()
-1
8 年前
回复了 Eric Duminil 创建的主题 » 在python中将字符串值拆分为整数

您应该首先围绕空格拆分以获取行,然后迭代这些行以获取单元格:

>>> rows = "2,3,4,5,6,7,8,9 2,3,4,5,6,7,8,9 2,3,4,5,6,7,8,9 2,3,4,5,6,7,8,9".split(' ')
>>> rows
['2,3,4,5,6,7,8,9', '2,3,4,5,6,7,8,9', '2,3,4,5,6,7,8,9', '2,3,4,5,6,7,8,9']
>>> [row.split(',') for row in rows]
[['2', '3', '4', '5', '6', '7', '8', '9'], ['2', '3', '4', '5', '6', '7', '8', '9'], ['2', '3', '4', '5', '6', '7', '8', '9'], ['2', '3', '4', '5', '6', '7', '8', '9']]

您还可以将这些单元格转换为整数:

>>> [[int(cell) for cell in row.split(',')] for row in rows]
[[2, 3, 4, 5, 6, 7, 8, 9], [2, 3, 4, 5, 6, 7, 8, 9], [2, 3, 4, 5, 6, 7, 8, 9], [2, 3, 4, 5, 6, 7, 8, 9]]

还是让 numpy 为您进行转换:

>>> import numpy as np
>>> np.array([row.split(',') for row in rows], np.int)
array([[2, 3, 4, 5, 6, 7, 8, 9],
       [2, 3, 4, 5, 6, 7, 8, 9],
       [2, 3, 4, 5, 6, 7, 8, 9],
       [2, 3, 4, 5, 6, 7, 8, 9]])