-1

I am trying to split a list every 5th item, then delete the next two items ('nan'). I have attempted to use List[:5], but that does not seem to work in a loop. The desired output is: [['1','2','3','4','5'],['1','2','3','4','5'],['1','2','3','4','5'],['1','2','3','4','5']]

List = ['1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan']

for i in List:
    # split first 5 items
    # delete next two items

# Desired output:
# [['1','2','3','4','5'],['1','2','3','4','5'],['1','2','3','4','5'],['1','2','3','4','5']]
Colt
  • 23
  • 1
  • 6
  • Did you try anything, do any research? As an aside, don't name a variable `list`, and certainly not `List`. – AMC Jan 22 '20 at 19:46
  • Does this answer your question? [How do you split a list into evenly sized chunks?](https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks) – AMC Jan 22 '20 at 19:46
  • Also this one: https://stackoverflow.com/q/9671224/11301900 – AMC Jan 22 '20 at 19:46
  • @AMC - not quite a duplicate, you would still need to account for the `nan's` – wwii Jan 22 '20 at 19:51

9 Answers9

4

There are lots of ways to do this. I recommend stepping by 7 then splicing by 5.

data = ['1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan']

# Step by 7 and keep the first 5
chunks = [data[i:i+5] for i in range(0, len(data), 7)]

print(*chunks, sep='\n')

Output:

['1', '2', '3', '4', '5']
['1', '2', '3', '4', '5']
['1', '2', '3', '4', '5']
['1', '2', '3', '4', '5']

Reference: Split a python list into other “sublists”...

1

WARNING: make sure the list follows the rules as you said, after every 5 items 2 nan.

This loop will add the first 5 items as a list, and delete the first 7 items.

lst = ['1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan']
output = []

while True:
    if len(lst) <= 0:
        break

    output.append(lst[:5])
    del lst[:7]

print(output) # [['1', '2', '3', '4', '5'], ['1', '2', '3', '4', '5'], ['1', '2', '3', '4', '5'], ['1', '2', '3', '4', '5']]
Jeeva
  • 1,029
  • 3
  • 15
  • 21
0

Generally numpy.split(...) will do any kind of custom splitting for you. Some reference:

https://docs.scipy.org/doc/numpy/reference/generated/numpy.split.html

And the code:

import numpy as np

lst = ['1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan']

ind=np.ravel([[i*7+5, (i+1)*7] for i in range(len(lst)//7)])

lst2=np.split(lst, ind)[:-1:2]

print(lst2)

Outputs:

[array(['1', '2', '3', '4', '5'], dtype='<U3'), array(['1', '2', '3', '4', '5'], dtype='<U3'), array(['1', '2', '3', '4', '5'], dtype='<U3'), array(['1', '2', '3', '4', '5'], dtype='<U3')]
Grzegorz Skibinski
  • 12,624
  • 2
  • 11
  • 34
0
List=['1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan','1','2','3','4','5','nan','nan']
new_list = list()
for k in range(len(List)//7):
    new_list.append(List[k*7:k*7+5])
new_list.append(List[-len(List)%7])
tard
  • 107
  • 6
  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – Ismael Padilla Jan 22 '20 at 23:06
0

Straightforward solution in case if the list doesn’t follow the rules you mentioned but you want to split sequence always between NAN's:

result, temp = [], []

for item in lst:
    if item != 'nan':
        temp.append(item)
    elif temp:
        result.append(list(temp))
        temp = []
Charnel
  • 4,222
  • 2
  • 16
  • 28
0

Using itertools.groupby would also support chunks of different lengths:

[list(v) for k, v in groupby(List, key='nan'.__ne__) if k]
Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65
0

I guess there is more pythonic way to do the same but:

result = []
while (len(List) > 5):
    result.append(List[0:0+5])
    del List[0:0+5]
    del List[0:2]

This results: [['1', '2', '3', '4', '5'], ['1', '2', '3', '4', '5'], ['1', '2', '3', '4', '5'], ['1', '2', '3', '4', '5']]

Milos K
  • 1,009
  • 8
  • 5
0
mainlist=[] 
sublist=[] 
count=0

for i in List:
    if i!="nan" :    
        if count==4:
            # delete next two items
            mainlist.append(sublist)
            count=0
            sublist=[] 

        else:
            # split first 5 items
            sublist.append(i)
            count+=1
Renaud
  • 2,709
  • 2
  • 9
  • 24
0

I like the splice answers.

Here is my 2 cents.

# changed var name away from var type
myList = ['1','2','3','4','5','nan','nan','1','2','3','4','10','nan','nan','1','2','3','4','15','nan','nan','1','2','3','4','20','nan','nan']
newList = []   # declare new list of lists to create
addItem = []   # declare temp list
myIndex = 0    # declare temp counting variable

for i in myList:
    myIndex +=1
    if myIndex==6:
        nothing = 0  #do nothing
    elif myIndex==7: #add sub list to new list and reset variables
        if len(addItem)>0:
            newList.append(list(addItem))
            addItem=[]
            myIndex = 0
    else:
       addItem.append(i)

#output
print(newList)
Brian
  • 238
  • 1
  • 4