Python:练习题(列表推导式、词频统计、异常处理、正则表达式等)

来源:互联网 发布:arpu值算法 编辑:程序博客网 时间:2024/06/16 06:06

题目涉及到的知识点

  • 表达式与函数:题目1、2
  • 列表切片、推导式:题目3.1、3.2、5
  • 函数:题目4、5
  • 词频统计:题目6、7、8
  • 类与对象:题目8
  • 异常处理:题目9、10
  • 正则表达式:题目7、8、11、12
  • 文件读写:题目10、11、12

题目1

任意定义三个数(有整型和浮点型),通过比较判断,输出其最大者。

a=5b=6c=4.0if a>b:    if a>c:        print a    else:        print celse:    if b>c:        print b    else:        print c

题目2

改写上道题,写一个函数,输出三个输入值的最大值

def max_num(a,b,c):    if a>b:        if a>c:            print a        else:            print c    else:        if b>c:            print b        else:            print c#测试函数max_num(5,6.0,4.0)

题目3.1

用list comprehension生成1-20000之间所有能被3整除不能被5整除的数

new_list=[tmp for tmp in range(1,20001) if tmp%3==0 and tmp%5!=0]

题目3.2

练习切片,取出上述列表中前10个数,最后5个数,下标为偶数的数,并把列表逆序

#前10个数print new_list[:10]#最后5个数print new_list[-5:]#下标为偶数的数print new_list[0::2]#把列表逆序print new_list[::-1]

题目4

定义一个函数,完成一个小任务:对于给定的银行定期利率(输入),计算多少年后可以连本带息翻番

import math#输入参数为年利率def deposit_double(interest_rate):    return math.ceil(math.log(2,(1+interest_rate)))#测试函数deposit_double(0.015)

题目5

一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如,6的因子为1、2、3,而6=1+2+3,因此6是完数。编程,找出10000之内的所有完数,并输出该完数及对应的因子。

#求一个数的所有因子def get_factors(num):    return [tmp for tmp in range(1,num) if num%tmp==0]#求小于num的所有完数def get_perfect_numbers(num):    return [[tmp,get_factors(tmp)] for tmp in range(1,num) if sum(get_factors(tmp))==tmp]#测试函数get_perfect_numbers(10000)

题目6

摘录网页HTML源码,粘贴到input.txt,统计其中英文字母、空格、数字和其他字符的个数并输出。

infile=open('input.txt','r')letter_count=0strip_count=0digit_count=0other_count=0for line in infile:    for ch in line:        if ch>='a'and ch<='z' or ch>='A'and ch<='Z':            letter_count+=1        elif ch>='0' and ch<='9':            digit_count+=1        elif ch==' ':            strip_count+=1        else:            other_count+=1print 'The counts of letter are %d'%letter_countprint 'The counts of digit are %d'%digit_countprint 'The counts of strip are %d'%strip_countprint 'The counts of other character are %d'%other_count

题目7

在网上摘录一段英文文本(尽量长一些),粘贴到input.txt,统计其中每个单词的词频(出现的次数),并按照词频的顺序写入out.txt文件,每一行的内容为“单词:频次”

import reinfile=open('input.txt','r')#全文字符串txt_str=""for line in infile:    txt_str+=line#转为小写txt_str=txt_str.lower()#将所有单词装入一个listword_list=re.findall(r'\b[a-zA-Z]+\b',txt_str)#统计词频word_count={}for word in word_list:    if not word_count.has_key(word):        word_count[word]=word_list.count(word)#排序sorted_word_count=sorted(word_count.items(),key=lambda item:item[1],reverse=True)#写入文件outfile=open('out.txt','w')for word_count in sorted_word_count:    outfile.write(word_count[0]+':'+str(word_count[1])+'\n')outfile.close()

题目8

王同学希望用电脑记录他每天掌握的英文单词。请设计程序和相应的数据结构,使小王能记录新学的英文单词和其中文翻译,并能很方便地根据英文来查找中文。实现一个类,能完成功能:1.背单词(添加单词) 2.查单词(根据单词进行翻译) 3.修改单词含义 4.统计历届四六级试卷,查找高频词,按照高频词排序

# encoding: utf-8class DanCiBen(object):    def __init__(self,my_dict={}):        self.my_dict=my_dict    def memorize(self,word,meaning):        self.my_dict[word]=meaning    def search(self,word):        if word in self.my_dict.keys():            return self.my_dict[word]        else:            return "这个单词你还没背哦~"    def modify(self,word,meaning):        self.my_dict[word]=meaning    def get_hot_words(self):        txt_list=['./cet/2010.06.txt','./cet/2010.12.txt',                  './cet/2011.06.txt','./cet/2011.12.txt',                  './cet/2012.06.txt','./cet/2012.12.txt',                  './cet/2013.06.txt','./cet/2013.12.txt',                  './cet/201006.txt','./cet/201012.txt',                  './cet/201106.txt','./cet/201112.txt',                  './cet/201206.txt','./cet/201212.txt',                  './cet/201306.txt','./cet/201312.txt',                  './cet/2014.06.txt','./cet/201406.txt',]        #将所有的文本存入一个字符串        full_txt_str=""        for txt_name in txt_list:            infile=open(txt_name,'r')            for line in infile:                full_txt_str+=line        #转为小写        full_txt_str=full_txt_str.lower()        #将所有单词装入一个list        word_list=re.findall(r'\b[a-zA-Z]+\b',full_txt_str)        #统计词频        word_count={}        for word in word_list:            if not word_count.has_key(word):                word_count[word]=word_list.count(word)        #排序        sorted_word_count=sorted(word_count.items(),key=lambda item:item[1],reverse=True)        return sorted_word_count#类测试dcb=DanCiBen()#查单词print "hello:"+dcb.search('hello')#背单词dcb.memorize('hello','你好')dcb.memorize('world','单词')#查单词print "hello:"+dcb.search('hello')print "world:"+dcb.search('world')#修改单词含义dcb.modify('world','世界')#查单词print "world:"+dcb.search('world')#词频排序dcb.get_hot_words()

题目9

写一个函数接收两个整数,并输出相加结果。但如果输入的不是整数(如字母、浮点数等),程序就会终止执行并输出异常信息(异常处理)。请对程序进行修改,要求输入非整数时,给出“输入内容必须为整数!”的提示,并提示用户重新输入,直至输入正确。

def int_sum():    while True:        try:            x=input("请输入x:")            if type(x)==int:                break            else:                raise Exception()        except:            print "输入内容必须为整数!"    while True:        try:            y=input("请输入y:")            if type(y)==int:                break            else:                raise Exception()        except:            print "输入内容必须为整数!"    print str(x)+ "+"+ str(y) + "=" + str(x+y)int_sum()

题目10

请输入一个文件路径名或文件名,查看该文件是否存在,如存在,打开文件并在屏幕上输出该文件内容;如不存在,显示“输入的文件未找到!”并要求重新输入;如文件存在但在读文件过程中发生异常,则显示“文件无法正常读出!”并要求重新输入。(提示:请使用异常处理。“文件未找到”对应的异常名为:FileNotFoundError,其他异常直接用except匹配)

import osclass FileNotFoundError(Exception):    def __int__(self,arg=[]):        self.args=argwhile True:    try:        file_name=raw_input("请输入一个文件路径名或文件名:")        if os.path.exists(file_name):            infile=open(file_name,'r')            for line in infile:                print line            break        else:            raise FileNotFoundError()    except FileNotFoundError:        print "输入的文件未找到!"    except:        print "文件无法正常读出!"

题目11

找一本英文小说(转为txt格式),把其中所有的代词(I, you, he, she, it, we, you, they, me, you, him, her, it, us, you, them)都替换成**

import reinfile=open('novel.txt','r')#全文字符串txt_str=""for line in infile:    txt_str+=linep = re.compile(r'\bI\b|\byou\b|\bshe\b|\bit\b|\bwe\b|\bthey\b|\bme\b|\bher\b|\bus\b|\bthem\b|\bhim\b|\bhe\b')print p.sub(lambda x:"**", txt_str)

题目12

找出文件夹下所有文件名格式为output_YYYY.MM.DD.txt(output_2016.10.21.txt)的文件 。读取文件名中的日期时间信息,并找出这一天是周几。将文件改名为output_YYYY-MM-DD-W.txt (YYYY:四位的年,MM:两位的月份,DD:两位的日,W:一位的周几,并假设周一为一周第一天)

import reimport osimport datetimep=re.compile(r'(^output_\d{4})(\.)(\d{2})(\.)(\d{2})(\.txt$)')def func(m):    year=int(m.group(1)[-4:])    month=int(m.group(3))    day=int(m.group(5))    time=datetime.datetime(year, month, day)    weekday=time.weekday()+1    return m.group(1) + '-' + m.group(3)+'-'+m.group(5)+'-'+str(weekday)+m.group(6)for parent, dirnames, filenames in os.walk('./question12'):        for filename in filenames:          if p.match(filename):              newName = p.sub(func,filename)              print filename+" ----> "+ newName              os.rename(os.path.join(parent, filename), os.path.join(parent, newName))
原创粉丝点击