Home > database >  I want to group data in a list with missing values as delimiter
I want to group data in a list with missing values as delimiter

Time:01-27

I have a sample data like this:

data = [1,2,3,nan,nan,nan,4,5,6,nan,nan,7,nan,8,9]

I want results like this:

group1 = [1,2,3]
group2 = [4,5,6]
group3 = [7]
group4 = [8,9]

But in fact I have a lot of information. can you help me ?

CodePudding user response:

If nan is the math.nan type, this would be a way of doing it. If it's not, you can just use a different comparison when it checks if the element is nan. This approach would return a list of lists.

from math import isnan, nan

data = [1,2,3,nan,nan,4,5,6,nan,nan,7,nan,8,9]

current_list = []
res = []
for ele in data:
    if not isnan(ele): # Your comparison here
        current_list.append(ele)
    else:
        if current_list:
            res.append(current_list)
            current_list = []
if current_list:
    res.append(current_list)
print(res)
>>> [[1, 2, 3], [4, 5, 6], [7], [8, 9]]

CodePudding user response:

Every time you want to group something, you probably want to go for itertools.groupby or collections.defaultdict:

from math import nan

data = [1, 2, 3, nan, nan, nan, 4, 5, 6, nan, nan, 7, nan, 8, 9]

from itertools import groupby

g = groupby(data, key=type)  # group by type of the data, int != float

groups = [list(a[1]) for a in g if a[0] != float]  

print(groups)  

Output:

[[1, 2, 3], [4, 5, 6], [7], [8, 9]]

CodePudding user response:

With functools.reduce:

from functools import reduce

nan = None
data = [1,2,3,nan,nan,nan,4,5,6,nan,nan,7,nan,8,9]

def myReducer(x, y):
    if y!= nan:
        x[-1].append(y)
    else:
        if x[-1] != []:
            x.append([])
    return x

reduce(myReducer, data, [[]])

Output:

[[1, 2, 3], [4, 5, 6], [7], [8, 9]]

Note: the array (iterable) is not the first argument of reduce, the reducer function is.

  •  Tags:  
  • Related