python: extract integers from mixed list

前端 未结 4 839
无人及你
无人及你 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 12:46

    as explained by @pts for isinstance, so use type like this

    [ x for x in testList if type(x)==int ]
    

    output:

    [1, 7, 5, 7]
    

    use set to remove duplication

提交回复
热议问题