python在eureka中注册_Eureka在Python中的时刻

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值