python中的列表推到式属于其特有的语法,可以使代码变得简洁很多,如下demo:
# 将一个1-8的list映射为其平方的list
nums = range(1, 9, 1)
quares = [num**2 for num in nums]
print(quares)
# wow,so cool
quares = [num**2 for num in nums if num % 2 == 0]
print(quares)
cities = ["beijing", "new york city", "tokyo", "london"]
capitals = [city.title() for city in cities]
print(capitals)