python语法小杂记

prices = {
    '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 prices.items() if value >100}
print(price2)

price2 = {key: value for key, value in prices.items() if value<100}
print(price2)

从上面可以看出Python中定义了一个变量可以重复使用,但是后面的赋值会覆盖掉之前的赋值

你可能感兴趣的:(python)