Design a logical expression equivalent to the following statement:
x
is a list of three or five elements, the second element of which is the string'Hip'
and the first of which is not a number or Boolean.
我有什么:
x = ['Head', 'Hip', 10]
print x[1] is 'Hip'
我的问题:如何检查它是 bool 值还是数字?
最佳答案
回答具体问题:
isinstance(x[0], (int, float))
这会检查 x[0]
是否是元组 (int, float)
中任何类型的实例。
您也可以在其中添加 bool
,但这不是必需的,因为 bool
本身就是 int
的子类。
文档引用:
要评论您当前的代码,您不应依赖 interning of short strings .您应该使用 ==
运算符比较字符串:
x[1] == 'Hip'
关于python - 检查对象是数字还是 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15019830/