I''d be interested in hearing people''s stories of Eureka moments in Python,
moments where you suddenly realise that some task which seemed like it
would be hard work was easy with Python.
I had a recent one, where I had spent some time creating a function which
took a list as an argument, and then rearranged the items according to
what magicians call a Monge shuffle. I had started off with a piece of
code that walked the list, dropping items into two new lists, then
combining them again. I got the code down to eight lines or so, something
like this:
# from memory, untested
top, bottom = True, False # not strictly needed, but helps document code
where = bottom
holder = {top: [], bottom: []}
for item in reversed(alist):
holder[where].append(item)
where = not where
holder[bottom].reverse()
print holder[bottom] + holder[top]
And then it suddenly struck me that I could do it as a one-liner:
alist[1::2] + list(reversed(alist[0::2]))
Or if I wanted to be even more concise, if slightly less readable:
alist[1::2] + alist[0::2][::-1]
If only a real Monge shuffle with actual cards was so easy...
--
Steven D''Aprano
解决方案Steven D''Aprano wrote:
I''d be interested in hearing people''s stories of Eureka moments in Python,
moments where you suddenly realise that some task which seemed like it
would be hard work was easy with Python.
Mine was definitely when I was first working with the xmlrpc module, and
I was puzzled to no end how it was that I was able to make a call to a
method of an object when that method didn''t exist. All the talk about
Python being a dynamic language had bounced off of me until that moment,
when I walked through the __getattr__ method in the debugger and was
enlightened.
Not surprisingly, that was also the moment when C++ started feeling like
a straightjacket to me...
On Mar 13, 10:05 am, Brett g Porter
Steven D''Aprano wrote:
I''d be interested in hearing people''s stories of Eureka moments in Python,
moments where you suddenly realise that some task which seemed like it
would be hard work was easy with Python.
Mine was definitely when I was first working with the xmlrpc module, and
I was puzzled to no end how it was that I was able to make a call to a
method of an object when that method didn''t exist. All the talk about
Python being a dynamic language had bounced off of me until that moment,
when I walked through the __getattr__ method in the debugger and was
enlightened.
Not surprisingly, that was also the moment when C++ started feeling like
a straightjacket to me...
Started? Meaning there was a time when it DIDN''T feel like a
straightjacket? If I were a journalist, this would be all over the
news...
On Mar 13, 2:16 am, Steven D''Aprano
wrote:
I''d be interested in hearing people''s stories of Eureka moments in Python,
moments where you suddenly realise that some task which seemed like it
would be hard work was easy with Python.
## I have this problem where, given a list of non-zero
## positive integers (sv), I need to calculate threee constants
## x, y, z where
##
## x is 2 raised to the power of the sum of the elements in sv,
##
## y is 3 raised to the power of the count of the elements in sv,
##
## and
##
## z is 3**a*2**A + 3**b*2**B + ... + 3**m*2**M + 3**n*2**N
##
## where a,b,...m,n are 0,1,...len(sv)-2,len(sv)-1 and
## N is the sum of all elements of sv except the last one,
## M is the sum of all elements of sv except the last two,
## ...
## B is the sum of all elements of sv except the last len(sv)-1,
## A is the sum of all elements of sv except the last len(sv).
##
## This turned out to be one of those things that''s
## easier to code than to describe.
##
## And if you saw my original Excel spreadsheet where I
## developed it, you''d say Eureka too.
def calc_xyz(sv):
x = 2**sum(sv)
y = 3**len(sv)
z = 0
for i in xrange(len(sv)):
z += 3**i * 2**sum(sv[:-(i+1)])
return (x,y,z)
sv = [1,2,3,4]
print calc_xyz(sv)
## (1024, 81, 133)