Handling lists

Mage mage at mage.hu
Sat Apr 23 16:14:11 EDT 2005


superprad at gmail.com wrote:

>I have a question on python lists.
>Suppose I have a 2D list
>list = [[10,11,12,13,14,78,79,80,81,300,301,308]]
>how do I convert it so that I arrange them into bins  .
>so If i hvae a set of consecutive numbers i would like to represent
>them as a range in the list with max and min val of the range alone.
>I shd get something like 
>list = [[10,14],[78,81],[300,308]]
>
>  
>
Maybe:

list = [10,11,12,13,14,78,79,80,81,300,301,308]

new_list = []
start = 0
for i in range(1,len(list) + 1):
    if i == len(list) or list[i] - list[i-1] <> 1:
        new_list.append([list[start],list[i-1]])
        start = i

print new_list
   



More information about the Python-list mailing list