Chrisvdberge
Chrisvdberge

Reputation: 1956

How to nest generators

I'm looking into generators to make some of my code more efficient. Currently wondering how I can/should handle nested for loops that each create a list. The nested loop re-generates a list of values for every step of the for loop. These values are a vector normalization of 3 columns of the fed data. From this list a max value is taken and added to the list that is returned by the function. Seems this could be made much more efficient by using generators. I'm not sure however how to handle the nested for loop as the function will stop on the first yield.

Code as it is currently;

def define_max_values(data, indexes):

    max_values = []

    for iter in range(len(indexes)):
        data_index_window = data[index[iter] - 5:index[iter] + 50]
        for i in range(len(data_index_window)):
            sub_list = []   
            sub_list.append(np.linalg.norm(data_index_window.iloc[i, 0:3].values))

        max_values.append(max(sub_list))
    return max_values

I think this could be turned into this, using 1 generator

def define_max_values(data, indexes):
     for iter in range(len(indexes)):
            data_index_window = data[index[iter] - 5:index[iter] + 50]
            for i in range(len(data_index_window)):
                sub_list = []
                sub_list.append(np.linalg.norm(data_index_window.iloc[i, 0:3].values))

            yield(max(sub_list)

This seems more readable/efficient code already, but wondering if/how I could replace the inner list.append with another yield.

Upvotes: 0

Views: 114

Answers (1)

khachik
khachik

Reputation: 28703

max doesn't need a sequence, it can be used with generators as well, something like:

def define_max_values(data, indexes):
    for iter in range(len(indexes)):
        data_index_window = data[index[iter] - 5:index[iter] + 50]
        yield max(np.linalg.norm(
                          data_index_window.iloc[i, 0:3].values) \
                  for i in range(len(data_index_window)))

Upvotes: 1

Related Questions