问题描述
我有兴趣听听人们关于Eureka在Python中的时刻的故事,
你突然意识到某些任务看起来像什么的时候
使用Python会很容易的工作。
我最近有一个,我花了一些时间创建一个函数
将列表作为一个列表参数,然后重新排列项目按照
魔术师称之为Monge shuffle。我开始使用一段
代码走进列表,将项目放入两个新列表,然后
再次组合它们。我把代码缩小到八行左右,这样的东西
就像这样:
#来自内存,未经测试
top, bottom = True,False#不是严格需要的,但有助于文档代码
where = bottom
holder = {top:[],bottom:[]}
for reverse(alist)中的项目:
持有人[where] .append(item)
where = not where
holder [底部] .reverse()
打印支架[底部] +支架[顶部]
然后它突然让我觉得我可以做一个单线程:
alist [1 :: 2] + list(reverse(alist [0 :: 2]))
或者如果我想要更简洁,如果可读性稍差:
alist [1 :: 2] + alist [0 :: 2] [:: - 1]
>
如果只有真正的Monge随身携带实际卡牌那么容易......
-
Steven D'' Aprano
当我第一次使用xmlrpc模块的时候,我的确是绝对的。
我很困惑,我到底是怎么回事当该方法不存在时,能够调用对象的
方法。所有谈论
Python是一种动态语言,直到那一刻我才退出,
当我在调试器中走过__getattr__方法时,
是
开明。
毫不奇怪,这也是C ++开始感觉像我这样的直接夹克的时刻......
当我第一次使用xmlrpc模块时,我的确是绝对的。
我很困惑,我到底是怎么回事当该方法不存在时,能够调用对象的
方法。所有谈论
Python是一种动态语言,直到那一刻我才退出,
当我在调试器中走过__getattr__方法时,
是
开明。
毫不奇怪,这也是C ++开始感觉像我这样的b $ ba直筒衣的时刻...
开始了?意思是有一段时间它感觉像是一件
的紧身衣?如果我是一名记者,这将是
新闻...
##我有这个问题,给出一个非零的列表
##正整数(sv) ,我需要计算三个常数
## x,y,z其中
##
## x是2加权sv中元素总和的数量,
##
## y是3增加到sv中元素数的幂,
##
##和
##
## z是3 ** a * 2 ** A + 3 * * b * 2 ** B + ... + 3 ** m * 2 ** M + 3 ** n * 2 ** N
##
##其中a,b,... m,n为0,1,... len(sv)-2,len(sv)-1和
## N是除了最后一个之外的sv的所有元素的总和,
## M是除了最后两个之外的sv的所有元素的总和,
# #...
## B是除了最后一个len(sv)-1之外的所有sv元素的总和,
## A是所有元素的总和sv除了最后一个len(sv)。
##
##原来这是'
$ b $的东西之一b ##比编码更容易编码。
##
##如果你看到我原来的Excel电子表格我在哪里
##开发它,你也会说Eureka。
def calc_xyz(sv):
x = 2 ** sum(sv)
$ b $ = 3 ** len(sv)
z = 0
$ x $ b for x in xrange(len(sv)):
z + = 3 ** i * 2 **总和(sv [:-(i + 1)])
返回(x,y,z)
sv = [1,2,3,4]
print calc_xyz(sv)
##(1024,81,133)
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
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...
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...
## 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)
这篇关于Eureka在Python中的时刻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!