Check multidimensional list in Python

I have some data that is either 1 or 2 dimensional. I want to iterate over each pattern in a dataset and do foo () on it. If the data is 1D, then add this value to the list, if it is 2D, take the average from the internal list and add this value. I saw this question and decided to implement it by checking, for example, the list. I can not use numpy for this application.

    outputs = []
    for row in data:
        if isinstance(row, list):
            vals = [foo(window) for window in row]
            outputs.append(sum(vals)/float(len(vals)))
        else:
            outputs.append(foo(row))

Is there an easier way to do this? In each run, each template will have the same dimension, so I could create a separate class for 1D / 2D, but this will add many classes to my code. Datasets can be quite large, so a quick fix is ​​preferable.

+3
source share
1 answer

Your code is almost as fast and fast as it can be. The only slight improvement is to replace [foo(window) for window in row]with map(foo, row), which can be seen from the standards:

> python -m timeit "foo = lambda x: x+1; list(map(foo, range(1000)))"
10000 loops, best of 3: 132 usec per loop
> python -m timeit "foo = lambda x: x+1; [foo(a) for a in range(1000)]"
10000 loops, best of 3: 140 usec per loop

isinstance()It already seems faster than its counterparts hasattr()and type() ==:

> python -m timeit "[isinstance(i, int) for i in range(1000)]"
10000 loops, best of 3: 117 usec per loop
> python -m timeit "[hasattr(i, '__iter__') for i in range(1000)]"
1000 loops, best of 3: 470 usec per loop
> python -m timeit "[type(i) == int for i in range(1000)]"
10000 loops, best of 3: 130 usec per loop


However, if you think that short as neat, you can also simplify your code (after replacing map) with:

mean = lambda x: sum(x)/float(len(x)) #or `from statistics import mean` in python3.4
output = [foo(r) if isinstance(r, int) else mean(map(foo, r)) for r in data]
+2
source

All Articles