Nested Loop In Python With Lists Input

- 1 answer

I want to have an array of signals which calculated from a function, by loop through different parameter combination, something like below:

fast_w = list(range(20, 100, 20))
slow_w = list(range(100, 200, 20))
tradeSignal = np.zeros((len(fast_w), len(slow_w)))
for i in fast_w:
    for j in slow_w:
        tradeSignal[i][j] = signalTrade(i, j, stock_price, end_date)

however "tradeSignal[i][j]" is incorrect as i and j would be the values in the fast_w and slow_w list, which here it suppose to be the index of array tradeSignal

So what is the right way to write such code? new to python and its package ....thanks for help



How about this? I'm not certain that it works, since we're missing part of your code, but it should.

trade_signal = np.fromfunction(lambda i, j: signalTrade(i, j, stock_price, end_date), shape=(4, 5))

When working with numpy, you should avoid explicit loops and iteration unless absolutely necessary.