将浮点数转换为百分比(%):

rate = [ 0.413, 0.457, 0.253 ]
percentage = []
for n in rate:
percentage.append(n*100)
print percentage # [41.3, 45.7, 25.3]

使用列表推导式:

rate = [ 0.413, 0.457, 0.253 ]
percentage = [ n*100 for n in rate ]
print percentage # [41.3, 45.7, 25.3]

同样我们还能使用推导式创建集合:

num_list = [1, 2, 3, 4, 5, 2, 5, 1, 4, 8]
num_set = { x for x in some_list if x % 2 == 0 }
print num_set # set([8, 2, 4])

推导式创建字典

num_dict = { x: x % 2 == 0 for x in range(1, 11) }
print num_dict # {1: False, 2: True, 3: False, 4: True, 5: False, 6: True, 7: False, 8: True, 9: False, 10: True}