How to see if the list contains consecutive numbers

后端 未结 12 1411
别跟我提以往
别跟我提以往 2021-02-07 12:50

I want to test if a list contains consecutive integers and no repetition of numbers. For example, if I have

l = [1, 3, 5, 2, 4, 6]

It should re

12条回答
  •  春和景丽
    2021-02-07 13:12

    list must be sorted!

    lst = [9,10,11,12,13,14,15,16]
    
    final = True if len( [ True for x in lst[:-1] for y in lst[1:] if x + 1 == y ] ) == len(lst[1:]) else False
    

    i don't know how efficient this is but it should do the trick.

提交回复
热议问题