如何检查列表中包含的类型?

时间:2013-01-06 11:45:32

标签: python error-reporting

我想在不定义函数的情况下执行以下操作:

if isinstance(x,(list,tuple)) and every_element_isinstance(x,basestring):
   foobar

ie:implementing type checking

这是否有简写/ builtin

4 个答案:

答案 0 :(得分:8)

我认为这是最好的解决方案(如果我理解了这个问题)

if isinstance(x, (list, tuple)) and all(isinstance(i, basestring) for i in x):
    #do whatever

答案 1 :(得分:5)

if isinstance(x, (list, tuple)) and all([isinstance(i, basestring) for i in x]):
    foobar

令人惊讶的是,使用[ ... ]的列表理解比没有列表理解更快,包括短列表和长列表:

简短列表:

>>> timeit('isinstance(x, (list, tuple)) and all(isinstance(i, basestring) for i in x)', "x=['a','b','c']")
2.7594685942680144
>>> timeit('isinstance(x, (list, tuple)) and all(isinstance(i, basestring) for i in x)', "x=['a','b','c']")
2.8013695153947538
>>> timeit('isinstance(x, (list, tuple)) and all([isinstance(i, basestring) for i in x])', "x=['a','b','c']")
2.4351678506033068
>>> timeit('isinstance(x, (list, tuple)) and all([isinstance(i, basestring) for i in x])', "x=['a','b','c']")
2.4491469896721583

长名单:

>>> timeit('isinstance(x, (list, tuple)) and all(isinstance(i, basestring) for i in x)', "x=['a','b','c'] * 1000", number=1000)
1.3357901657891489
>>> timeit('isinstance(x, (list, tuple)) and all(isinstance(i, basestring) for i in x)', "x=['a','b','c'] * 1000", number=1000)
1.3305278872818462
>>> timeit('isinstance(x, (list, tuple)) and all([isinstance(i, basestring) for i in x])', "x=['a','b','c'] * 1000", number=1000)
1.2626525921055531
>>> timeit('isinstance(x, (list, tuple)) and all([isinstance(i, basestring) for i in x])', "x=['a','b','c'] * 1000", number=1000)
1.2881240045551863

答案 2 :(得分:4)

没有内置来定义generic types。但是有a lot of validation libraries,它可以模仿这个功能。

使用https://github.com/alecthomas/voluptuous的示例:

>>> from voluptuous import Schema
>>> s_list = Schema([basestring]) # only strings in a list are allowed
>>> s_list("hello") 
...
voluptuous.InvalidList: expected a list
>>> s_list([123])
...
voluptuous.InvalidList: invalid list value @ data[0]
>>> s_list(["correct"])
["correct"] # returns the object, if validation was successful

几天前,support for tuple was added到这个图书馆:

>>> s_tuple = voluptuous.Schema((basestring, ))

现在将两者结合起来得到你的结果:

>>> from voluptuous import any

# - this is now equivalent to your code
# - raises Exceptions on invalid input
>>> schema = Schema(any(s_list, s_tuple))

双倍isinstance

的速度甚至更快
>>> from timeit import timeit

>>> timeit('(schema(i) for i in x)', "x=['a','b','c']")
0.679318904876709

>>> timeit("""
        (isinstance(x, (list, tuple)) 
         and all(isinstance(i, basestring)) for i in x)""", "x=['a','b','c']")
0.7801780700683594

答案 3 :(得分:0)

凭借性感的'0.8.7',您可以更新miku的答案并跳过“元组部分”:

>>> from voluptuous import Schema
>>> from timeit import timeit
>>> s_list = Schema([basestring]) # only strings in a list are allowed
>>> timeit('(s_list(i) for i in x)', "x=['a','b','c']")
0.503572940826416
>>> timeit("(isinstance(x, (list, tuple)) and all(isinstance(i, basestring)) for i in x)", "x=['a','b','c']")
0.5400209426879883