How to test if every item in a list of type 'int'?

后端 未结 7 2018
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 16:21

Say I have a list of numbers. How would I do to check that every item in the list is an int?
I have searched around, but haven\'t been able to find anything on this.

7条回答
  •  孤城傲影
    2020-12-08 16:57

    lst = [1,2,3]
    lst2 = [1,2,'3']
    
    list_is_int = lambda lst: [item for item in lst if isinstance(item, int)] == lst
    
    print list_is_int(lst)
    print list_is_int(lst2)
    
    suxmac2:~$ python2.6 xx.py 
    True
    False
    

    ....one possible solution out of many using a list comprehension or filter()

提交回复
热议问题