函数练习题2

时间:2023-02-23 11:12:41

1.生成器本质上也是一种迭代器,可用__next__唤醒并执行。唯一的区别在于实现方式上,迭代器更加简洁。

用__next__唤醒并执行获取,通过for循环来获取指定数量的值。

2生成器日志调用方法(未完成)

3.内置函数

1.

a = map(lambda x:x + "_ss",["eee","fffff","fggg"])
print(list(a))
 #['eee_ss', 'fffff_ss', 'fggg_ss']

 

2.

def func(n):
    if n%2 == 0:
        return n

a = filter(func,[1,3,5,6,7,8])
print(list(a))

3.

portfolio = [
    {"name": "dfff", "shares": 100,"price": 555},       #计算购买每只股票的总价
    {"name": "fggggg", "shares": 444,"price": 55544},       #用filter过滤出单只股票价格大于100的股票
    {"name": "fgg", "shares": 66, "price": 6666},
    {"name": "ggg", "shares": 12, "price": 77},
    {"name": "hhhh", "shares": 44, "price": 8888}

]
m = map(lambda x: x["shares"]*x["price"],portfolio)
print(list(m))
n = filter(lambda x:x["price"]>=1000,portfolio)
print(list(n))
[55500, 24661536, 439956, 924, 391072]
[{'name': 'fggggg', 'shares': 444, 'price': 55544}, {'name': 'fgg', 'shares': 66, 'price': 6666}, {'name': 'hhhh', 'shares': 44, 'price': 8888}]