Check if all elements of a list are of the same type

前端 未结 9 1806
盖世英雄少女心
盖世英雄少女心 2020-11-28 05:08

How can I check if the elements of a list are of the same type, without checking individually every element if possible?

For example, I would like to have a function

9条回答
  •  旧巷少年郎
    2020-11-28 05:47

    >>> def checkInt(l):
        return all(isinstance(i, (int, long)) for i in l)
    
    >>> checkInt([1,2,3])
    True
    >>> checkInt(['a',1,2,3])
    False
    >>> checkInt([1,2,3,238762384762364892364])
    True
    

提交回复
热议问题