(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:
testList = [1, 4.66, 7, "abc", 5, True, 3.2, False, "Hello", 7]
I thought this would be simple, and just wrote:
def parseIntegers(mixedList):
newList = [i for i in mixedList if isinstance(i, int)]
return newList
Problem is that the newList this creates has boolean values as well as integers, meaning it gets me:
[1, 7, 5, True, False, 7]
Why is that? I also used for loop (for i in mixedList: if isinstace.....), but it's essentially the same(?) and has the same problem.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…