生成式(推到式)的用法
price = {
'AAPL': 191.88,
'GOOG': 1186.96,
'IBM': 149.24,
'ORCL': 48.44,
'ACN': 166.89,
'FB': 208.09,
'SYMC': 21.29
}
# 用股票价格大于100元的股票构造一个新的字典
price2 = {key: value for key, value in price.items if value > 100}
print(price)
函数enumerate()
函数enumerate()用于可迭代的对象,返回索引下标及索引值:
list1 = ['zero', 'one', 'two', 'three']
for i, number in enumerate(list1):
# list1[0] is zero
# list1[1] is one
# list1[2] is two
# list1[3] is three
print('list1[%d] is %s' % (i, number))