编码风格 [英] Coding style

查看:76
本文介绍了编码风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个更好?


lst = [1,2,3,4,5]


而lst:

lst.pop()





而len(lst)0:

lst.pop()

解决方案

2006年7月17日08:56:34 -0700,PTY< ty **** *@yahoo.com写道:


哪个更好?


lst = [1,2,3,4,5 ]


而lst:

lst.pop()





而len(lst)0:

lst.pop()



如何


lst = [1,2,3,4,5]

而lst:

lst.pop()


甚至只是:


lst = []


;-)


-

干杯,

Simon B,
si *** @ brunningonline.net
HTTP://www.brunningonlin e.net/simon/blog/


PTY写道:


哪个更好?


lst = [1,2,3,4,5]


而lst:

lst。 pop()





而len(lst)0:

lst.pop()



前者毫无疑问。它说的完全一样,因为当它为空时,它只能被认为是假的。经验丰富的Python

程序员会在你的第二个表述中摸不着头脑。


我怀疑从时间的角度来看它有多少(虽然我知道我这个写了这个会刺激别人用timeit.py指出我错了。


问候

史蒂夫

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC / Ltd http://www.holdenweb.com

Skype:holdenweb http://holdenweb.blogspot.com

最近的Ramblings http://del.icio.us/steve.holden


lst = [1,2,3,4,5]


而lst:

lst.pop()


甚至只是:


lst = []



虽然有点不同......


>> while lst:



.... lst.pop ()

....

5

4

3

2

1


>> lst2



[]


>> lst = [1,2,3,4,5]
lst2 = lst
lst = []
lst2



[1,2,3,4,5]


>> lst = [1,2,3,4,5]
lst2 = lst
del lst [:]
lst2



[]


原始while循环更改实际列表,重新分配

到新列表会阻止其他引用该列表的项目从

访问更改。如上所示,我推荐


del lst [:]


这应该和python一样快。 (也许?

再次与那些timeit家伙...;)


-tkc



Which is better?

lst = [1,2,3,4,5]

while lst:
lst.pop()

OR

while len(lst) 0:
lst.pop()

解决方案

On 17 Jul 2006 08:56:34 -0700, PTY <ty*****@yahoo.comwrote:

Which is better?

lst = [1,2,3,4,5]

while lst:
lst.pop()

OR

while len(lst) 0:
lst.pop()

How about:

lst = [1,2,3,4,5]
while lst:
lst.pop()

Or even just:

lst = []

;-)

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/


PTY wrote:

Which is better?

lst = [1,2,3,4,5]

while lst:
lst.pop()

OR

while len(lst) 0:
lst.pop()

The former, without a doubt. It says exactly the same thing, since lst
can only be considered false when it is empty. Experienced Python
programmers would scratch their heads at your second formulation.

I doubt there''s much in it from a time point of view (though I know as I
write this it will spur someone to use timeit.py to point out I am wrong).

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden


lst = [1,2,3,4,5]

while lst:
lst.pop()

Or even just:

lst = []

Subtly different though...

>>while lst:

.... lst.pop()
....
5
4
3
2
1

>>lst2

[]

>>lst = [1,2,3,4,5]
lst2 = lst
lst = []
lst2

[1, 2, 3, 4, 5]

>>lst = [1,2,3,4,5]
lst2 = lst
del lst[:]
lst2

[]

The original while loop changes the actual list, reassigning it
to a new list prevents other items that reference that list from
accessing the changes. As shown above, I recommend

del lst[:]

which should be as fast as python will let one do it. (maybe?
again with those timeit guys... ;)

-tkc



这篇关于编码风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆