學習Python必刷的100道經典練習題,沒練等於白學!!!_哔哩哔哩_bilibili
手敲代碼如下:
#列別去重
from pickletools import read_uint1
def ss(lis):
r=[]
for i in lis:
if i not in r:
r.append(i)
return r
li=[3,22,2,22,1]
sdsad=[33,22,1,44]
print(li,ss(li))
print(sorted(sdsad))#對list排序
#字典 元組排序
dict_shh=[{'score':1,'score':3,'score':2}]
sor=sorted(dict_shh,key=lambda x:x['score'])
#讀取TXT文件,並把\n換行符去掉,且按某值排序
def read_file():
res=[]
with open("./rr.txt") as fin:
for line in fin:
line=line[:-1]#把結尾\n換行符去掉
res.append(line,split(","))#數據間隔用, 去掉“,”
return res
def sort_g(datas):
return sorted(datas,key=lambda x: int(x[2]))
def write_f(datas):
with open ("./rrinput.txt","w") as fount:
for data in datas:
fount.write(",".join(data)+"\n")
#同時輸出三個值
def com_score():
scor=[]
with open("./rr.txt") as fin:
for line in fin:
line=line[:-1]#把結尾\n換行符去掉
fields=line.split(",")
scor.append(int(fields[-1]))#取最後一位,並把str轉換為int
maxS=max(scor)
mins=min(scor)
avs=round(sum(scor)/len(scor),2)
return maxS,mins,avs
#統計英語文字中出現最多的單詞數目
#方法1
def mostString():
dict = {}
fr = open('preprocessing.txt')
k = 0
n = 0
for line in fr.readlines():
for s in line.strip().split(' '):
if s not in dict.keys():
dict[s] = 0
else:
dict[s] = dict[s] + 1
if dict[s]>=n:
k = s
n = dict[s]
#print(dict)
print(k)
方法2
word_count={}
with open("./untitled.txt") as fin:
for line in fin:
line=line[:-1]#把結尾\n換行符去掉
words=line.split()
for word in words:
if word not in word_count:
word_count[word]=0
word_count[word]+=1
tt=sorted(word_count.items(),key=lambda x:x[1],reverse= True)[:10]
print(tt)
#讀取文件夾下文件
from importlib.resources import contents
import os
import shtuli
dir="./sss"
sum_size=0
for file in os.listdir(dir):
if os.path.isfile(file):
sum_size+=os.path.getsize(file)#求文件大小
ext=os.path.splitext(file)[1]
ext=ext[1:]
if not os.path.isdir(f"{dir}/{ext}"):
os.mkdir(f"{dir}/{ext}")
source_path=f"{dir}/{file}"
target_path=f"{dir}/{ext}/{file}"
#遞歸搜索目錄,找出最大的文件
import os
# d:/workbenc/xxxsearch_dir = "/Users/peishuaishuai/workbench"
result_files = []
for root, dirs, files in os.walk(search_dir):
for file in files:
if file.endswith(".txt"):
file_path = f"{root}/{file}"
result_files.append((file_path,
os.path.getsize(file_path) / 1000))
sorted(result_files,key=lambda x: x[1],reverse=True)[:10]
#每個班級的最高最低平均分
def compute_quanbanscore():#讀取文件進行計算
course_grades=[]
with open("./student_grade_input",encoding="utf-8") as fin:#讀取文件,不設置編碼方式會亂碼
fin.readlines()
for line in fin:#讀取了每一行
line=line[:-1]#這個語法是將最後的換行符去掉
course,sno,sname,grade=line.split(",")
if course not in course_grades:
course_grades[course ]=[]
course_grades[course].append(grade)
for course,grades in course_grades.items():
print(
course,
max(grade) ,
min(grade) ,
sum(grade) /len(grade)
)
#實現不同文件關聯
course_tech_map={}
with open("./student_grade_input",encoding="utf-8") as fin:#讀取文件,不設置編碼方式會亂碼
for line in fin:#讀取了每一行
line=line[:-1]#這個語法是將最後的換行符去掉
course,teacher=line.split(",")
course_tech_map[course ]=teacher
with open("./student_grade_input")as fin:
for line in fin:#讀取了每一行
line=line[:-1]#這個語法是將最後的換行符去掉
course,sno,sname,sgrade=line.split(",")
teacher=course_tech_map.get(course)
print(course,teacher,sno,sname,sgrade)
#實現批量TXT文件合並
import os
datadir="./ssss/sss"
contents=[]
for file in os.listdir(datadir):
file_path=f"{datadir}/{file}"
if os.path.isfile(file_path) and file.endswith(".txt"):
print(file_path)
with open(file_path)as fin:
contents.append(fin.read())
final_context="\n".join(contents)
with open("./a/a.txt","w") as fout:
fout.write(final_context)
#統計每個興趣人數
like_coun={}
with open("./like.txt",encoding="utf-8") as fin:
for line in fin:
line=line[:-1]
sna,likes=line.split(",")
likelist=likes.split(",")
for like in likelist:
like_coun[like]=0
like_coun[like]+=1
print(like_coun)
# #獲取當前日期和時間
from ast import pattern
import datetime
curr_datetime=datetime.datetime.now()
str_time=curr_datetime.strftime("%Y-%m-%d %H:%M:%S")
curr_datetime.year
curr_datetime.month
curr_datetime.day
curr_datetime.hour
curr_datetime.minute
curr_datetime.second
#計算幾歲了
birthday="2022-01-02"
birthday_date=datetime.datetime.strptime(birthday,"%Y-%m-%d")
minus=curr_datetime-birthday_date
sui=minus/365
#計算任何日期前的幾天的日期
def get_days(pdate,days):
pdate_obj=datetime.datetime.strptime(pdate,"%Y-%m-%d")
time_gap=datetime.timedelta(days=days)
pdate_r=pdate_obj-time_gap
return pdate_r.strftime("%Y-%m-%d")
import datetime
from traceback import print_tb
from unittest import result
def getdate_range(bdate,edate):
datelist=[]
while bdate<=edate:
datelist.append(bdate)
bdate_obj=datetime.datetime.strptime(bdate,"%Y-%m-%d")
days_timedelta=datetime.timedelta(days=1)
bdate=(bdate_obj+days_timedelta).strftime("%Y-%m-%d")
return datelist
print(getdate_range("2022-01-05","2022-02-05"))
#unix時間戳轉換為日期
unix_time=1620747647
datetime_obj=datetime.datetime.fromtimestamp(unix_time)
datetime_str=datetime_obj.strftime("%Y-%m-%d %H:%M:%S")
#計算日期周同比
date_sale={}
is_first_line=True
with open("./like.txt",encoding="utf-8") as fin:
for line in fin:
if is_first_line:
is_first_line=False
continue
line=line[:-1]
date,salenum=line.split("\t")
date_sale[date]=float(salenum)
def get_days111(pdate,days):
currtime=datetime.datetime.strptime(pdate,"%Y-%m-%d")
timedelta1=datetime.timedelta(days=-days)
(currtime+timedelta1).strftime("%Y-%m-%d")
for date,salenum in date_sale.items():
date7=get_days111(date,7)
sale_num7=date_sale.get(date7,0)
if sale_num7==0:
print(date,salenum,0)
else:
weekdiff=(salenum-sale_num7)/sale_num7
print(date,salenum,sale_num7,weekdiff)
#正則表達式 年月日
import re
def date_is_right(date):
return re.match("\d{4}-\d{2}-\d{2}",date) is not None
date1="2020-05-20"
date2="202-06-01"
date_is_right(date1)
date_is_right(date2)
#正則表達式手機號
content="aadasd18888888888da;lskd;alkf1231231njdnfuisf15422222222"
pattern=r"1[3-9]\d{9}"
re.sub(pattern,r"\1*************",content)#手機號打馬賽克
results=re.findall(pattern,content)
for result in results:
print(result)
file_cont=""
with open("./like.txt",encoding="utf-8") as fin:
file_cont=fin.read()
results=re.findall(pattern,file_cont)
#正則表達式郵箱
pattern2=re.compile(r"""
[a-zA-Z0-9_-]+
@
[a-zA-Z0-9]+
\.a
[a-zA-Z]{2,4}
""",re.VERBOSE)
results1=pattern2.findall(content)
#正則表達式 檢查密碼
def check_mima(ps):
if not 6<=len(ps)<=20:
return False,"在6-20之間"
if not re.findall(r"[a-z]",ps):
return False,"少a-z"
if not re.findall(r"[A-Z]",ps):
return False,"少A-Z"
if not re.findall(r"[0-9]",ps):
return False,"少數字"
if not re.findall(r"[^0-9a-zA-Z]",ps):
return False,"少特殊符號"
#正則表達式 固定提取
txtxt="""買了1斤黃瓜花了8元
買了2斤花生花了7.5元"""
for line in txtxt.split("\n"):
pattern3=r"\d斤.*花了\d+(\.\d+)?元"#第一位\d 數字,.*任何字符,\d數字+(\.\d+)? 轉義. 後面數字可有可無,+一個或多個
pattern4=r"(\d)斤(.*)花了(\d+(\.\d+)?)元"#()分組
match=re.search(pattern4,line)
if match:
print(f"{match.group(1)}\t{match.group(2)}\t{match.group(3)}")
#正則表達式 日期統一格式
cont="""2012/06/01,2014.05.06,05-28-2020,5/29/2022"""
con=re.sub(r"(\d{4})/(\d{2})/(\d{2})",r"\1-\2-\3",cont)
con=re.sub(r"(\d{4})\.(\d{2})\.(\d{2})",r"\1-\2-\3",cont)
con=re.sub(r"(\d{2})-(\d{2})-(\d{2})",r"\3-\1-\2",cont)
con=re.sub(r"(\d{1})/(\d{2})/(\d{2})",r"\3-0\1-\2",cont)
#英語單詞分詞
words=re.split(r"[\s.()-?]+,",cont)#\s空格等一系列特殊符號
import pandas as pd
pd.Series(words).value_counts()[:20]#前二十個
#中文分詞
comzn=""" 李明給韓梅梅說,北國風光,千裡冰封,萬裡雪飄。
望長城內外,惟余莽莽;大河上下,頓失滔滔。
山舞銀蛇,原馳蠟象,欲與天公試比高。
須晴日,看紅裝素裹,分外妖娆。
江山如此多嬌,引無數英雄競折腰。
惜秦皇漢武,略輸文采;唐宗宋祖,稍遜風騷。"""
comzn1=re.sub(r"[\s。,、]",comzn)
import jieba
word_list=jieba.cut(comzn)
#人名
import jieba.posseg as posseg
for word,flag in posseg.cut(comzn):
if flag=="nr":
print(word,flag)