码迷,mamicode.com
首页 > 其他好文 > 详细

会写代码和写好代码的区别

时间:2014-11-08 23:45:55      阅读:404      评论:0      收藏:0      [点我收藏+]

标签:blog   for   on   2014   log   代码   as   python   app   

功能:

对一个列表中的字符串小写

代码一:

l = ['Hello','World',20,'IBM','Apple']
J = []
for s in l:
	if isinstance(s,str):
		s.lower()
		J.append(s.lower())
	else:
		s
		J.append(s)
print J

代码二:

M = ['Hello','World',20,'IBM','Apple']
N = [h.lower() if isinstance(h,str) else h for h in M]
print N

执行的结果都是一致的,但是从代码量上来看就很明显,一个仅短短的3行代码就能搞定的,而另一个却写了10行。

写代码一定要简洁,记住这点,你才能写出高质量的代码!


会写代码和写好代码的区别

标签:blog   for   on   2014   log   代码   as   python   app   

原文地址:http://blog.csdn.net/yifei325325/article/details/40928931

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!