python: extract integers from mixed list

前端 未结 4 831
无人及你
无人及你 2020-12-22 12:02

(python 2.7.8)

I\'m trying to make a function to extract integers from a mixed list. Mixed list can be anything but the e.g. I\'m going with is:

test         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-22 13:02

    Apparently bool is a subclass of int:

    Python 2.7.3 (default, Feb 27 2014, 19:58:35) 
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> isinstance(42, int)
    True
    >>> isinstance(True, int)
    True
    >>> isinstance('42', int)
    False
    >>> isinstance(42, bool)
    False
    >>> 
    

    Instead of isinstance(i, int), you can use type(i) is int or isinstance(i, int) and not isinstance(i, bool).

提交回复
热议问题