Ad
Nested Loop In Python With Lists Input
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
Ad
Answer
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.
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad