第9课时字符提取专题答案及解析

第9课时字符提取专题答案及解析新质力文库 - 聚焦新质生产力发展的数字化知识库_行业洞察 / 理论成果 / 实践指南免费下载新质力文库
第9课时字符提取专题答案及解析
此内容为付费资源,请付费后查看
1131
立即购买
您当前未登录!建议登陆后购买,可保存购买订单
付费资源

第1页 / 共4页

第2页 / 共4页
试读已结束,还剩2页,您可下载完整版后进行离线阅读
THE END
乐清中学2024学年高二信息备课组校本作业课时9算法程序实现的棕合应用—一字符提取【例题】查找在一行文字查找指定的单词,若找到,则返回该单词第一次出现的文字,找不到则返回“找不到该单词”。It was Sunday.I never get up early on Sundays.I sometimes stay in bed until lunch time.Last Sunday I got up very late.I looked out of the window.It was dark outside.What a day!"I thought.'It's raining again.'Just then,the telephone rang.It was my aunt Lucy.'I've just arrived by train,'she said.I'm coming to see you.'"But I'm still having breakfast,'I said.What are you doing ?"she asked.'I'm having breakfast,I repeated.Dear me,'she said.'Do you always get up so late?It's one o'clock!"t1.xt内容如上图所示It was Sunday.I never get up early on Sundays.I sometimes stay in bed until lunch time.请输入要查找的单词:early要找的单词第一次出现的位置是第30位程序运行界面如上图所示代码1(字符连接法)程序如下:f=open('tl.txt','r')line=f.readline()print(line)c-input("请输入要查找的单词:")flag=Falsep0s=0;s=';t=for i in range(len(line)):ch=line[i]if'A'<=ch<=Z'or'a'<=ch<='Z':tt+ch或t+=chelse:ift==c and flag==False此处加brea可flag=True以让循环停pos=i-len(t)+1或pos=iHen(c)+1找到就不找if flag:print("要找的单词第一次出现的位置是第",pos,"位")else:print("找不到该单词")方框处代码替换成以下代码2(字符定位法1):p0s=0;s=';t=';j=0for i in range(len(line))ch=line[i]if not('A'<=ch<='Z'or'a'<=ch<=Z):t=line[j:i]j=计1ift==c and flag==False:flag=Truepos=i-len(c)+169
喜欢就支持一下吧
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容