Why is there difference between cmd line and .py file?

Robert rxjwg98 at gmail.com
Tue Jan 5 16:05:45 EST 2016


On Tuesday, January 5, 2016 at 3:58:44 PM UTC-5, Joel Goldstick wrote:
> On Tue, Jan 5, 2016 at 3:45 PM, Robert <rxjwg98 at gmail.com> wrote:
> 
> > On Tuesday, January 5, 2016 at 3:37:53 PM UTC-5, John Gordon wrote:
> > > In <e9f1d94f-4dd6-49b5-bf5c-bbcdc8d304e8 at googlegroups.com> Robert <
> > r-- at gmail.com> writes:
> > >
> > > > ////////////
> > > > # represent the experiments
> > > > head_counts = np.array([5,9,8,4,7])
> > >
> > > The code doesn't define 'np', so this line should produce an error.
> > >
> > > The code you linked contains this import:
> > >
> > >     import numpy as np
> > >
> > > However you didn't show it here, so I wonder if you posted the real code.
> > >
> > > > sum(expectation_A)[0]
> > > >
> > ---------------------------------------------------------------------------
> > > > IndexError                                Traceback (most recent call
> > last)
> > > > <ipython-input-145-d6f33dff0343> in <module>()
> > > > ----> 1 sum(expectation_A)[0]
> > >
> > > > IndexError: invalid index to scalar variable.
> > > > //////////////
> > >
> > > The built-in function sum() returns a single value, not a list, so this
> > > is a reasonable error.
> > >
> > > I suspect the code actually intended to call numpy.sum(), which does
> > > return a list (or something like a list).
> > >
> > > --
> > > John Gordon                   A is for Amy, who fell down the stairs
> > > gordon at panix.com              B is for Basil, assaulted by bears
> > >                                 -- Edward Gorey, "The Gashlycrumb Tinies"
> >
> > Thanks, John. When I typed my new thought, your reply came. You are right.
> >  The attached message missed numpy import (In my file, it had).
> >
> > Now, I cannot use np.sum. It has an error, see below please. How can I use
> > the numpy sum()?
> >
> > Thanks,
> > /////////
> > import numpy as np
> >
> > In [154]: np.sum(expectation_A)[0]
> > ---------------------------------------------------------------------------
> > IndexError                                Traceback (most recent call last)
> > <ipython-input-154-1e2cbda689a3> in <module>()
> > ----> 1 np.sum(expectation_A)[0]
> >
> > IndexError: invalid index to scalar variable.
> >
> 
> I've not used numpy, but you should print expectation_A to see what's in
> it.  It may be empty, causing the index.  It may be that expectation_A is
> an integer, not a list
> 
> >
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
> 
> 
> 
> -- 
> Joel Goldstick
> http://joelgoldstick.com/stats/birthdays

Thanks. It has sum method. OK now.



More information about the Python-list mailing list