python学习 day17 作业讲解

1. 用map来处理字符串列表,把列表中的所有人都变成sb,比如alex_sb

name=['alex','wupeiqi','yuanhao','nezha']
name=['alex','wupeiqi','yuanhao','nezha']
generator=map(lambda x:x+'sb',name)  #map函数返回的是一个generator
print(list(generator))   #使用强制类型转换可以打印其中的值

运行结果:

2.用filter函数处理数字列表,将列表中的所有偶数筛选出来

generator=filter(lambda x:x%2==0,[1,3,5,6,7,8])   #filter函数返回一个迭代器 generator
for i in generator:
    print(i)

运行结果:

3.随意写一个20行以上的文件,运行程序,先将内容读到内存中,使用列表存储,接收用户输入页码,每页5条。仅输出当页的内容

def func():
    content=[]
    with open('info',mode='r',encoding='utf-8') as file:
        for line in file:
            content.append(line.strip())
    return content

def get_msg():
    msg=int(input('please input the number of page(1-4):'))
    content=func()
    view_content=content[(msg-1)*5:msg*5]
    for i in view_content:
        print(i)
get_msg()

运行结果:

4.如下,每个小字典的name对应股票名字,shares对应多少股,prices对应股票的价格:

portfolio=[
    {'name':'IBM','shares':100,'price':91.1},
    {'name':'AAPL','shares':50,'price':543.22},
    {'name':'FB','shares':200,'price':21.09},
    {'name':'HPQ','shares':35,'price':31.75},
    {'name':'YHOO','shares':45,'price':16.35},
    {'name':'ACME','shares':75,'price':115.65},    
]

4.1 计算购买每只股票的总价;

4.2 用filter过滤出,单价大于100的股票有哪些

portfolio=[
        {'name':'IBM','shares':100,'price':91.1},
        {'name':'AAPL','shares':50,'price':543.22},
        {'name':'FB','shares':200,'price':21.09},
        {'name':'HPQ','shares':35,'price':31.75},
        {'name':'YHOO','shares':45,'price':16.35},
        {'name':'ACME','shares':75,'price':115.65},
    ]
def func1():
    price_total=0
    for i in portfolio:
        price_total+=i['price']
    return price_total
price_total=func1()
print(price_total)

def func2():
    generator=filter(lambda dict:dict['price']>100,portfolio)
    for i in generator:
        print(i['name'])
func2()

第一问,出题的语文水平一定不高,微笑.jpg

所以我的理解就是每种股票都买一股,然后这六种股票的总价~

猜你喜欢

转载自www.cnblogs.com/xuanxuanlove/p/9615074.html
今日推荐