[Numpy-discussion] Easter Egg or what I am missing here?

Chris Barker chris.barker at noaa.gov
Wed May 21 18:27:59 EDT 2014


On Wed, May 21, 2014 at 12:38 PM, alex <argriffi at ncsu.edu> wrote:

> > years = [2004,2005,2006,2007]
> >
> > dates = [20040501,20050601,20060801,20071001]
> >
> > for x in years:
> >
> >      print 'year ',x
> >
> >      xy =  np.array([x*1.0e-4 for x in dates]).astype(np.int)
> >
> >      print 'year ',x
>

did you mean that to be "print 'year' xy" I then get:

year  2004
year  [2004 2005 2006 2007]
year  2005
year  [2004 2005 2006 2007]
year  2006
year  [2004 2005 2006 2007]
year  2007
year  [2004 2005 2006 2007]

or di you really want something like:

In [35]: %paste
years = [2004,2005,2006,2007]

dates = [20040501,20050601,20060801,20071001]

for x, d in zip(years, dates):
     print 'year ', x
     print 'date', d
     print int (d*1.0e-4)
     print 'just date:', d - x*1e4

## -- End pasted text --
year  2004
date 20040501
2004
just date: 501.0
year  2005
date 20050601
2005
just date: 601.0
year  2006
date 20060801
2006
just date: 801.0
year  2007
date 20071001
2007
just date: 1001.0

but using floating point for this is risky anyway, why not:

In [47]: d
Out[47]: 20071001

In [48]: d // 10000
Out[48]: 2007

i.e integer division.

-Chris









> > ==
> >
>
>
> It seems like a misunderstanding of Python scoping, or just an
> oversight in your code, or I'm not understanding your question.  Would
> you expect the following code to print the same value twice in each
> iteration?
>
> for x in (1, 2, 3):
>     print x
>     dummy = [x*x for x in (4, 5, 6)]
>     print x
>     print
>
>
> > Or is this a recipe to blow up a power plant?
>
> Now we're on the lists...
>
>
> Cheers!
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140521/dd952b7d/attachment.html>


More information about the NumPy-Discussion mailing list