Easter Egg or what I am missing here?

Please would anyone tell me the following is an undocumented bug otherwise I will lose faith in everything: == import numpy as np 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 == Or is this a recipe to blow up a power plant? Thanks, Siegfried -- The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.

On Wed, May 21, 2014 at 3:29 PM, Siegfried Gonzi <siegfried.gonzi@ed.ac.uk> wrote:
Please would anyone tell me the following is an undocumented bug otherwise I will lose faith in everything:
== import numpy as np
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 ==
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!

On Wed, May 21, 2014 at 12:38 PM, alex <argriffi@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@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@noaa.gov

On 5/21/14, Siegfried Gonzi <siegfried.gonzi@ed.ac.uk> wrote:
Please would anyone tell me the following is an undocumented bug otherwise I will lose faith in everything:
== import numpy as np
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 ==
Or is this a recipe to blow up a power plant?
This is a "wart" of Python 2.x. The dummy variable used in a list comprehension remains defined with its final value in the enclosing scope. For example, this is Python 2.7:
x = 100 w = [x*x for x in range(4)] x 3
This behavior has been changed in Python 3. Here's the same sequence in Python 3.4:
x = 100 w = [x*x for x in range(4)] x 100
Guido van Rossum gives a summary of this issue near the end of this blog: http://python-history.blogspot.com/2010/06/from-list-comprehensions-to-gener... Warren
Thanks, Siegfried
-- The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (4)
-
alex
-
Chris Barker
-
Siegfried Gonzi
-
Warren Weckesser