YoungGu

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

1. range xrange 的差别

1.1 range 返回列表对象.

1.2 xrange 返回xrange对象  不需要返回列表里面的值, 节省内存.

>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> xrange(1,10)
xrange(1, 10)

2. 列表推导

>>> [x*x for x in range(1,10)]
[1, 4, 9, 16, 25, 36, 49, 64, 81]
#占位符应用
>>> ['the %s' %d for d in range(1,10)]
['the 1', 'the 2', 'the 3', 'the 4', 'the 5', 'the 6', 'the 7', 'the 8', 'the 9']
#元组
>>> [(x,y) for x in range(3) for y in range(3)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
#DICT
>>> dict([(x,y) for x in range(3) for y in range(3)])
{0: 2, 1: 2, 2: 2}
#Key, 读了后面的值, 更新了..

 

posted on 2016-02-12 20:21  YoungGu  阅读(285)  评论(0编辑  收藏  举报