连接: http://blog.lerner.co.il/understanding-nested-list-comprehensions-in-python/
列表推导的核心点就是: 迭代器
(1) 常规实现
>>> ret = []
>>> for line in open('/etc/passwd'):
... ret.append(len(line))
(2)列表推导实现
>>> [len(line) for line in open('/etc/passwd')]
其实line是一个字符串,string对象,在python里面也是可以迭代的。所以可以嵌套跌套,不过在嵌套推导前,先看一个小例子。
>>> [(x,y) for x in range(5) for y in range(5)]
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4)]
这个的常规实现就是:
>>> ret = []
>>> for x in range(5):
... for y in range(5):
... ret.append((x, y))
当然如果把上面的列表推导重新展示一下,如下:
>>> [(x, y)
... for x in range(5)
... for y in range(5)]
这下应该清晰很多了。我们来分解一下,具体是什么意思。
我们可以稍微修改一下
>>> [(x, y)
... for x in range(5)
... for y in range(x + 1)]
这里,第二个for,对y赋值取的是range(x + 1),也就是y小于等于x
基本信息如下:
>>> scores = {'Reuven':[300, 250, 350, 400],
... 'Atara':[200, 300, 450, 150],
... 'Shikma':[250, 380, 420, 120],
... 'Amotz':[100, 120, 150, 180] }
要求: 获取每个人的平均值
(1)常规方法
ret = dict()
>>> for name, score in scores.items():
... ret[name] = sum(score) / len(score)
(2)列表推导
>>> def average(input):
... return sum(input) / len(input)
>>> dict([(name, average(score)) for name, score in scores.items()])
{'Reuven': 325, 'Atara': 275, 'Amotz': 137, 'Shikma': 292}
如果你使用的python2.7或者3.1,上面的话,就不用转换,因为已经支持了字典推导式(Dictionary comprehensions)
>>> {name: average(score) for name, score in scores.items()}
如果想要获取所有人的平均值怎么办:
(1)常规方法:
>>> sum = 0
>>> plays_num = 0
>>> for one_player_score in scores.values():
... for score in one_player_score:
... sum += score
... plays_num += 1
...
>>> avg = sum / plays_num
(2)列表推导
>>> average([ one_score
... for one_player_scores in scores.values()
... for one_score in one_player_scores ])
257
是不是很简洁。
如果想要查大于200分的所有分数
>>> [ one_score
... for one_player_scores in scores.values()
... for one_score in one_player_scores
... if one_score > 200]
[300, 250, 350, 400, 300, 450, 250, 380, 420]
现在有一个酒店的住房信息,是一个list,每个list元素也是list,代表房间住房信息,list里面是字典,存放住过房客的信息。如下:
>>> rooms = [[{'age': 14, 'hobby': 'horses', 'name': 'A'},
{'age': 12, 'hobby': 'piano', 'name': 'B'},
{'age': 9, 'hobby': 'chess', 'name': 'C'}],
[{'age': 15, 'hobby': 'programming', 'name': 'D'},
{'age': 17, 'hobby': 'driving', 'name': 'E'}],
[{'age': 45, 'hobby': 'writing', 'name': 'F'},
{'age': 43, 'hobby': 'chess', 'name': 'G'}]]
获取酒店房客的名字
>>> [ person['name']
... for room in rooms
... for person in room]
['A', 'B', 'C', 'D', 'E', 'F', 'G']
获取喜欢下棋的人
>>> [ person['name']
... for room in rooms
... for person in room
... if person['hobby'] == 'chess' ]
本文只是一个学习笔记,如果了解详细,还请看文章开头的连接。