'I want to group data in a list with missing values as delimiter
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 ?
Solution 1:[1]
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]]
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Patrick Artner |
| Solution 2 | user2314737 |
