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()

运行结果:

版本二(Eva-J)---考虑了输入行数不是5的倍数的情况,就是有一页不足5行,需要单独考虑~

with open('info',mode='r',encoding='utf-8') as file:
    content=file.readlines()   #把文件的内容存成一个列表

page,mod=divmod(len(content),5)  # 使用divmod()函数求内容的行数和5的商,余数,表示有多少页
if mod!=0:
    page+=1
page_num=int(input('please input the number of page:'))
if page_num>page or page_num<=0:
    print("您输入的页码有误")
elif page_num==page and mod!=0:  #当输入的页码是最后一页,而且这个最后一页内容不足5行 也就是mod余数不为0
    for i in range(mod):
        print(content[(page-1)*5+i].strip())
else:                            #输入任意一页(页码合适范围,且每一页都是5行)
    for i in range(5):
        print(content[(page_num-1)*5+i].strip())

运行结果:

 

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

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

版本二(Eva-J)----第一问我的理解果然有偏差.jpg

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},
    ]

generator=map(lambda dic:{dic['name']:round(dic['shares']*dic['price'],2)},portfolio)
print(list(generator))

运行结果:

 

转载于:https://www.cnblogs.com/xuanxuanlove/p/9615074.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值