python return lists of continuous integers from list

后端 未结 1 1258
情书的邮戳
情书的邮戳 2020-12-09 22:59

I have a list of integers, and I want to generate a list containing a list of all the continuous integers.

#I have:
full_list = [0,1,2,3,10,11,12,59]
#I want         


        
相关标签:
1条回答
  • 2020-12-09 23:39

    You can use the following recipe:

    from operator import itemgetter
    from itertools import groupby
    full_list = [0,1,2,3,10,11,12,59]
    cont = [map(itemgetter(1), g) for k, g in groupby(enumerate(full_list), lambda (i,x):i-x)]
    # [[0, 1, 2, 3], [10, 11, 12], [59]]
    
    0 讨论(0)
提交回复
热议问题