Decompose a list of integers into lists of increasing sequences

前端 未结 6 926
北恋
北恋 2020-12-20 04:33

Assume no consecutive integers are in the list.

I\'ve tried using NumPy (np.diff) for the difference between each element, but haven\'t been able to use

6条回答
  •  不思量自难忘°
    2020-12-20 05:33

    Below code should help you. However I would recommend that you use proper nomenclature and consider handling corner cases:

    li1 = [6, 0, 4, 8, 7, 6]
    li2 = [1, 4, 1, 2, 4, 3, 5, 4, 0]
    
    def inc_seq(li1):
      lix = []
      li_t = [] 
      for i in range(len(li1)):
        #print (i)
        if i < (len(li1) - 1) and li1[i] >= li1[i + 1]:
          li_t.append(li1[i])
          lix.append(li_t)
          li_t = []
        else:
          li_t.append(li1[i])
    
    
      print (lix)
    
    inc_seq(li1)
    inc_seq(li2)
    

提交回复
热议问题