I have 2D data that I want to apply several functions to. The actual code uses xlrd
and .xlsx
file, but I provided the following boiler plate, to the solution was easily reproduced.
class Data: def __init__(self, value): self.value = value class Sheet: def __init__(self, data): self.data = [[Data(value) for value in row.split(',')] for row in data.split('\n')] self.ncols = max(len(row) for row in self.data) def col(self, index): return [row[index] for row in self.data]
Create sheet:
fake_data = '''a, b, c, 1, 2, 3, 4 e, f, g, 5, 6, i, , 6, , , , , ''' sheet = Sheet(fake_data)
In this object, data
contains a 2D array of strings (for input format), and I want to perform operations on the columns of this object. Nothing is controlled by me until this moment.
I want to do three things in this structure: wrap rows in columns, extract value
from each data
object, and try to convert the value to float
. If the value is not equal to float
, it should be converted to str
with white space selected.
from operators import attrgetter
You can see that map
is applied to each column twice. In other cases, I want to apply the function to each column more than two times.
Is there a better way to map multiple functions to iteration elements? Moreover, is it possible to avoid understanding the generator and directly apply the mapping to each iterable internal? Or is there a better and extensible way to get closer to this all together?
Please note that this question does not apply to xlrd
, this is just the current use case.