Home > Net >  How can I transform a list in a list of lists if I need to skip some characters in between?
How can I transform a list in a list of lists if I need to skip some characters in between?

Time:10-07

Let lst = [13, 1, 14, -64, 9, -64, 14, 5]

How can I create a list of lists without taking into account the negative number ?

Result would be [[13,1,14], [9], [14,5]]

lst = [13, 1, 14, -64, 9, -64, 14, 5] 
lst_index = []
for i,v in enumerate(lst):
    if lst[i] == -64:
        lst_index.append(i)

So this is what I have done to retrieve the index of each negative number. Now I should remove it from the list and create list of lists but how?? Thank you !

CodePudding user response:

Using itertools.groupby:

from itertools import groupby

lst = [13, 1, 14, -64, 9, -64, 14, 5]

res = [list(group) for key, group in groupby(lst, lambda x : x > 0) if key]
print(res)

Output

[[13, 1, 14], [9], [14, 5]]

Or simply:

current, res = [], []
for e in lst:
    if e < 0:
        if current:
            res.append(current)
            current = []
    else:
        current.append(e)

if current:
    res.append(current)

print(res)

Output

[[13, 1, 14], [9], [14, 5]]

CodePudding user response:

You could do something like this:

def group_positives(lst):
    result = []
    temp = []
    for value in lst:
        if value > 0:
            temp.append(value)
        else:
            if temp:
                result.append(temp)
                temp = []
    # Also append last temp if not empty
    if temp:
        result.append(temp)
    return result  

CodePudding user response:

One solution: every time you find a negative number add to the new list an empty list, and after that append the positive numbers to the last element of the new list, see below.

lst = [13, 1, 14, -64, 9, -64, 14, 5] 
new_lst = []
start_lst = True
for v in lst :
  if v < 0:
    start_lst = True
  else:
    if start_lst == True :
      start_lst = False
      new_lst.append([])
    new_lst[-1].append(v)

print(new_lst)

CodePudding user response:

What I do here is just adding The numbers to a New list till I find a negative number if a Negative number met The else Condition

1-Stop
2-Add the List you have to COMBO
3-Jump over this number by index 1

the last iteration will not add the last list because there is no negative number to Stop and do the 3 steps so I added -1 at the end to go to else

Summary Add Numbers to the list till you find a negative number then add this list to combo in the end, you will have the list but you will not add it combo as you didn't face a negative number ( so I added -1 )

 lst = [13, 1, 14, -64, 9, -64, 14, 5]
    
    
    def Test(V):
        V.append(-1)
        new = []
        Combo = []
        for index, value in enumerate(V):
            if(lst[index] > 0):
                new.append(lst[index])
            else:
                index  = 1
                Combo.append(new)
                new = list()
    
        return Combo


    z = Test(lst)
    print(z)

Output

   [[13, 1, 14], [9], [14, 5]]
  • Related