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

后端 未结 7 2026
没有蜡笔的小新
没有蜡笔的小新 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:59

    >>> my_list = [1, 2, 3.25]
    >>> all(isinstance(item, int) for item in my_list)
    False
    
    >>> other_list = range(3)
    >>> all(isinstance(item, int) for item in other_list)
    True
    >>> 
    

提交回复
热议问题