Check if the integer in a list is not duplicated, and sequential

Posted by prosseek on Stack Overflow See other posts from Stack Overflow or by prosseek
Published on 2010-03-11T22:11:56Z Indexed on 2010/03/12 0:47 UTC
Read the original article Hit count: 345

Filed under:

testGroupList is a list of integer. I need to check the numbers in testGroupList is sequential (i.e, 1-2-3-4...) and not duplicate numbers. Ignore the negative integer.

I implemented it as follows, and it's pretty ugly. Is there any clever way to do this?

    buff = filter(lambda x: x > 0, testGroupList) 
    maxval = max(buff) 
    for i in range(maxval): 
        id = i+1 
        val = buff.count(id) 
        if val == 1: print id, 
        elif val >= 2: print "(Test Group %d duplicated %d times)" % (id, val), 
        elif val == 0: print "(Test Group %d missing)" % id,

© Stack Overflow or respective owner

Related posts about python