Python作业【六】(语言练习题,稍有难度)

Day14(Python作业)

(Python作业来自qq群:651707058,欢迎任何程度的Python学习者)

题一:默认参数

请找出下面代码为什么会得出这样的结果。
'''

'现在我们来扩展一下列表相加,定义一个列表相加的函数,会给两个list后面都加一个None值作为结尾' \
'然后再相加'
'比如:L1 = [1,2] L2 = [3,4]  相加后变成 [1,2,None,3,4,None]' \
'L1 = [1,2] L2 = [] 相加后变成[1,2,None,None]' \
'L1 = [],L2=[],变成[None,None]'

def add_list(L1 = [],L2 = []):
    L1.append(None)
    L2.append(None)
    return L1+L2
print(add_list())
print(add_list([1,2]))
print(add_list([1,2],[3,4]))

'''
在函数的默认参数中,最好不要使用可变的默认参数
因为一个函数的默认参数的值,只会保留一份。
而不是在调用函数的时候临时去创建一个值(如下面的列表),

在以后调用函数的时候,函数都会去用这个 创建函数时创建的默认参数值
所以这一份默认参数值,如果是可变对象的话,调用就不敢保证默认参数是否
已经发生了改变,从而引起了函数错误

所以在为函数定义默认参数的时候,一定要保证默认参数值是一个不可变对象
'''

题二:歌词
我们知道一些播放器有桌面歌词的功能,他会在桌面显示一行歌词,之后又换成下一行歌词显示。在这里,我们需要使用Python来实现简单的显示歌词功能。
大概是这样的一个过程:
显示一行歌词->暂停1秒->换下一行歌词->暂停1秒->换下一行歌词。。。。

from time import sleep
with open('my way enlish.txt','r',encoding='utf-8') as f:
    words = f.readlines() #words是一个list,每个元素是一行
    #以上只是一个打开文件的方式,下面是本题的具体做法
    for line in words:
        print('\r',line[:-2],flush=True,end='')
        sleep(1.5)

'''
line[:-2] 表示去掉句子的最后一个标点符号和\n
'''

下面是我的答案:

import time
s = [
   'And now, the end is near',
'And so I face the final curtain',
'My friend, I\'ll make it clear',
'I\'ll state my case, of which I\'m certain',
'I\'ve lived a life that\'s full',
'I\'ve traveled each and every highway',
'And more, much more I did',
'I did it my way',
'Regrets, I\'ve had a few',
'But then again, too few to mention',
'I did what I had to do',
'And saw it through without exemption',
'I planned each charted course',
'Each careful step along the by way',
'and more, much more than this',
'I did it my way',
'Yes, there were times, I am sure you knew',
'When I bit off more than I could chew',
'But through it all,when there was doubt',
'I ate it up and spit it out',
'I faced it all and I stood tall',
'And did it my way',
'I’ve loved, I’ve laughed and cried',
'I’ve had my fill, my share of losing',
'And now, as tears subside',
'I find it all so amusing',
'To think I did all that',
'And may I say - not in a shy way',
'No, oh no not me',
'I did it my way',
'For what is a man, what has he got?',
'If not himself, then he has naught',
'To say the things he truly feels',
'And not the words of one who kneels',
'The record shows I took the blows',
'And did it my way!',
'Yes, it was my way',
]
for i in s[:]:
    print('\r',end='')#返回行首
    print(i,end='',flush=True)#实时输出
    time.sleep(1.5)#停顿1.5秒

题三:显示时间
有了上一题的基础,我们再做出一个动态时钟。只需输出一行 21:45:50 这就表示一个时钟,但是它是可以动的。也就是每隔1s动一下,来模拟时钟。

from time import sleep
hours = 21
minutes = 59
seconds = 50
while True:
    sleep(1)
    seconds +=1
    if seconds>=60:
        seconds %=60
        minutes+=1
        if minutes>=60:
            minutes%=60
            hours+=1
            if hours>=24:
                hours%=24
    print('\r','{:<02}:{:<02}:{:<02}'.format(hours,minutes,seconds),flush=True,end='')

题四:大文件读取
现在有一个很大的文本文件,它总共只有一行,也就是这庞大的数据是没有\n的
而其分割符为{|}.可以查看data.txt,
现在我们需要将文件读取,以{|}为行分隔符,一行一行复制写入到data_copy.txt
这个文件

注意:这里我们要假装这个文件很大(10GB),太大上传太慢。我们不能使用一次性读取的方法,因为我们电脑内存有限。

def read_bigdata(filename,lineflag):
    with open(filename,'r',encoding='utf-8') as f:
        buf = '' #定义一个缓冲
        while True:
            while lineflag in buf: #如果缓冲里面有行分隔符
                pos = buf.index(lineflag)
                yield buf[:pos]
                buf = buf[pos+len(lineflag):]
            buf2 = f.read(4096*10) #读取10个字节
            if not buf2:
                break #如果读完了,就结束
            buf+=buf2
if __name__ == '__main__':
    with open('data_copy.txt','w',encoding='utf-8') as f2:
        while True:
            res = read_bigdata('data.txt','{|}')
            for line in res:
                f2.write(line+'\n')
            break
    print('复制结束')

题目有点难度,我还不会做,以上是qq群主的答案,供大家参考,不懂的小伙伴们可以加群一起学习。

猜你喜欢

转载自blog.csdn.net/qq_42849332/article/details/81668845