taonico
taonico

Reputation: 107

Loop to split list in different sublists

I want to split one list of number comparing the first number of the sequence with the next one to figure out if it's bigger, in this case we create that sublist till find one number who is smaller. And each other

Inputdata = [45,42,78,120,47,2,50,32,34]

compare 45 with 42 is bigger not, so create list just with 45, and start with 42 to compare with the next one till find the smaller than 42:

Output = [45] [42,78,120,47] [2,50,32,34]

this is my code:

data = [45,42,78,120,47,2,50,32,34]

m = (len(data))
i=0
list1=[]
emptylist = True
while i <= m:

 for j in range(i+1,len(data)-1):


    if data[i] < data[j]:
        list1.append(data[j])
        emptylist = False

    else:

        if emptylist:

            list1.insert(0,data[i])
            print list1
            i += j
            list1 = []
            emptylist = True
            break

        else:
            list1.insert(0,data[i])
            print list1
            print j
            i += j
            print i
            list1 = []
            emptylist = True
            i += j
            break

My Output is:

[45]
[42, 78, 120, 47]

and I can't find the problem... any suggestions? thanks

PD1. The idea is create sublist where the first element is the smaller one.

Upvotes: 2

Views: 3267

Answers (1)

Mike M&#252;ller
Mike M&#252;ller

Reputation: 85492

L = [45, 42, 78, 120, 47, 2, 50, 32, 34]

cond = L[0]
res = [[cond]]
for item in L[1:]:
    if item > cond:
        res[-1].append(item)
    else:
        cond = item
        res.append([cond])

Now resis :

[[45], [42, 78, 120, 47], [2, 50, 32, 34]]

Details:

Indexing with 0 gives you the first element:

>>> cond = L[0]
>> cond
45

We create new list res that will hold our result and put the 45, i.e. the very first element into a list that in turn becomes the first element of res:

>>> res = [[cond]]
>>> res
[[45]]

Now, we iterate over the list starting form the second element. L[1:] gives this sublist:

>>> L[1:]
[42, 78, 120, 47, 2, 50, 32, 34]

Note, res[-1] always gives us the currently last element of a list. This itself must be list and we append the next number (item) if it is bigger than our cond. Otherwise, we append a new sublist with only one element res.append([cond]). During the next round we do the same. Therefore, the list that makes up the last element in our res will grow as long as item > cond. If not, we append a new list with cond. Rinse and repeat until the end of the original list.

Variation

The L[1:] makes a copy of L. This not the most efficient way of doing it. If we turn L into an iterator we can avoid this:

L = iter([45,42,78,120,47,2,50,32,34])
cond = next(L)
res = [[cond]]
for item in L:
    if item > cond:
        res[-1].append(item)
    else:
        cond = item
        res.append([cond])

Each next(L) will give us the next item in the iterator. The for-loop will go over the rest. So in our case it starts at the second element because we called next() once.

Upvotes: 4

Related Questions