相关疑难解决方法(0)

如何检查字符串是否为数字(浮点数)?

检查字符串是否可以在Python中表示为数字的最佳方法是什么?

我目前拥有的功能是:

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False
Run Code Online (Sandbox Code Playgroud)

这不仅是丑陋而且缓慢,似乎很笨重.但是我没有找到更好的方法,因为调用floatmain函数更糟糕.

python floating-point casting type-conversion

1519
推荐指数
21
解决办法
127万
查看次数

按谓词过滤Python列表

我想做的事情如下:

>>> lst = [1, 2, 3, 4, 5]
>>> lst.find(lambda x: x % 2 == 0)
2
>>> lst.findall(lambda x: x % 2 == 0)
[2, 4]
Run Code Online (Sandbox Code Playgroud)

在Python的标准库中是否有任何接近这种行为的东西?

我知道在这里滚动你自己很容易,但我正在寻找一种更标准的方式.

python

44
推荐指数
2
解决办法
4万
查看次数

标签 统计

python ×2

casting ×1

floating-point ×1

type-conversion ×1