Python:練習題(列表推導式、詞頻統計、異常處理、正則表達式等)

題目涉及到的知識點

  • 表達式與函數:題目1、2
  • 列表切片、推導式:題目3.1、3.2、5
  • 函數:題目4、5
  • 詞頻統計:題目6、7、8
  • 類與對象:題目8
  • 異常處理:題目9、10
  • 正則表達式:題目7、8、11、12
  • 文件讀寫:題目10、11、12

題目1

任意定義三個數(有整型和浮點型),通過比較判斷,輸出其最大者。

a=5
b=6
c=4.0

if a>b:
    if a>c:
        print a
    else:
        print c
else:
    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=0
strip_count=0
digit_count=0
other_count=0

for 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+=1


print 'The counts of letter are %d'%letter_count
print 'The counts of digit are %d'%digit_count
print 'The counts of strip are %d'%strip_count
print 'The counts of other character are %d'%other_count

題目7

在網上摘錄一段英文文本(儘量長一些),粘貼到input.txt,統計其中每個單詞的詞頻(出現的次數),並按照詞頻的順序寫入out.txt文件,每一行的內容爲“單詞:頻次”

import re
infile=open('input.txt','r')

#全文字符串
txt_str=""
for line in infile:
    txt_str+=line
#轉爲小寫
txt_str=txt_str.lower()
#將所有單詞裝入一個list
word_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-8

class 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 os

class FileNotFoundError(Exception):
    def __int__(self,arg=[]):
        self.args=arg

while 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 re
infile=open('novel.txt','r')

#全文字符串
txt_str=""
for line in infile:
    txt_str+=line
p = 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 re
import os
import datetime

p=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))
發佈了75 篇原創文章 · 獲贊 127 · 訪問量 40萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章