On Fri, Nov 13, 2015 at 8:37 AM, PythonDude <mjoerg.ph...@gmail.com> wrote: > Hi all, > > Just a quick question about this code-piece (it works, I've tested it): > > means, stds = np.column_stack([ > getMuSigma_from_PF(return_vec) > for _ in xrange(n_portfolios) ]) > > > 1) I understand column_stack does this (assembles vectors vertically, > side-by-side): > >>>> a = np.array((1,2,3)) # NB: a is row-vector: {1 2 3} >>>> b = np.array((2,3,4)) # NB: b is also a row-vector... >>>> np.column_stack((a,b)) > array([[1, 2], > [2, 3], > [3, 4]]) > > 2) I understand the underscore is just a "dummy variable" in the last line > "for _ in xrange(n_portfolios)" - this also looked a bit confusing to me, at > first... > > 3) I DON'T understand why the code doesn't look like this: > > means, stds = np.column_stack([ > for _ in xrange(n_portfolios): > getMuSigma_from_PF(return_vec) ])
Because that would be invalid syntax; you can't put a for loop inside an expression like that. Your question is not about numpy.column_stack at all, but about list comprehensions. I suggest you start by reading this: https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions Then if you're still confused, come back and ask further questions. -- https://mail.python.org/mailman/listinfo/python-list