网站开发的上市公司有哪些,重庆房地产信息官网,金山网站建设关键词排名,wordpress 控制每页显示文章数准备30万条新闻数据编号新闻类别新闻数量(条)1财经370982教育419633科技655344时政630865体育655346娱乐65534yield生成器斐波那契数列介绍和数学描述斐波那契数列算法实现斐波那契数列算法生成器实现算法时间性能对比分析# codingutf-8Description#xff…准备30万条新闻数据编号新闻类别新闻数量(条)1财经370982教育419633科技655344时政630865体育655346娱乐65534yield生成器斐波那契数列介绍和数学描述斐波那契数列算法实现斐波那契数列算法生成器实现算法时间性能对比分析# codingutf-8Descriptionyield生成器案例import time斐波那契数列1,1,2,3,5,8,13,21,34,55,89,144从数列的第三项开始后面每一项是前面两项之和数学上的定义F(0) 1, F(1) 1, F(n) F(n-1) F(n-2)(n2, n∈N)# 普通的斐波那契数列实现def fab1(max):n, a, b 0, 0, 1while n max:# print(-, b)a, b b, a bn n 1# 生成器算法实现斐波那契数列def fab2(max):n, a, b 0, 0, 1while n max:yield b # 使用yield生成器a, b b, a bn n 1def test():# 最大迭代数max_num 1000000start_time time.time()fab1(max_num)end_time time.time()print(fab1 total time %.2f % (1000 * (end_time - start_time)), ms)start_time time.time()# B为一个# 可以通过遍历进行获取值b fab2(max_num)end_time time.time()print(fab2 total time %.2f % (1000 * (end_time - start_time)), ms)if __name__ __main__:test()输出fab1 total time 7085.11 msfab2 total time 0.00 ms递归读取30万新闻实现文件遍历递归遍历读取30万新闻每万条读取打印一次完成30万新闻遍历读取#!/usr/bin/env python# -*- encoding: utf-8 -*-Description : 递归批量读取30W文本Time : 2020/1/2 23:41Author : skySite :File : FileRead.pySoftware: PyCharmimport osimport timedef traversal_dir(root_dir):返回指定目录包含的文件或文件夹名字列表:param root_dir: 根目录:return: 文件(文件夹)名字列表for index, file_name in enumerate(os.listdir(root_dir)):# 待处理文件名字列表child_file_path os.path.join(root_dir, file_name)if os.path.isfile(child_file_path):# 对文件进行操作if index % 10000 0:print({c} *** {t} *** {i} \t docs has been read.format(croot_dir, iindex, ttime.strftime(%Y-%m-%d %H:%M:%S, time.localtime())))elif os.path.isdir(child_file_path):# 递归遍历文件目录traversal_dir(child_file_path)if __name__ __main__:root_dir ../dataset/CSCMNewsstart_time time.time()traversal_dir(root_dir)end_time time.time()print(Total Cost Time %.2f % (end_time - start_time) s)高效读取30万新闻构建生成器构建迭代器高效读取30万新闻读取30万新闻性能对比#!/usr/bin/env python# -*- encoding: utf-8 -*-# Description 高效读取30万新闻# Time : 2020/1/4 14:47# Author : sky# Site :# File : EfficRead.py# Software: PyCharmimport osimport time# 迭代器class LoadFolders(object):def __init__(self, parent_path):self.parent_path parent_pathdef __iter__(self):for file in os.listdir(self.parent_path):file_abspath os.path.join(self.parent_path, file)if os.path.isdir(file_abspath):yield file_abspathclass LoadFiles(object):def __init__(self, parent_path):self.parent_path parent_pathdef __iter__(self):folders LoadFolders(self.parent_path)# 第一级目录for folder in folders:category folder.split(os.sep)[-1]# 第二级目录for file in os.listdir(folder):yield category, fileif __name__ __main__:file_path os.path.abspath(../dataset/CSCMNews)start_time time.time()files LoadFiles(file_path)for index, msg in enumerate(files):if index % 10000 0:print({t} *** {i} \t docs has bean read.format(ttime.strftime(%Y-%m-%d %H:%M:%S, time.localtime()), iindex))end_time time.time()print(Total Cost Time %.2f % (end_time - start_time) s)小结数组、链表、字符串、文件等缺点就是所有的海量的数据生成器是可以迭代的就是重复调用,直到没有下一个有不再是一个普通的函数而是一个可用于。yield是一个类似正则清洗数据分析新闻语料文本读取新闻文本信息正则过滤掉特殊符号、标点、英文、数字等正则去除空格、换行符、多空格合并等实现正则清洗文本数据正则表达式学习#!/usr/bin/env python# -*- encoding: utf-8 -*-# Description 正则表达式学习# Time : 2020/1/4 15:13# Author : sky# Site :# File : reqular.py# Software: PyCharmimport re# . 任意字符# * 任意次数# ^ 表示开头# $ 表示结尾# ? 非贪婪模式提取第一个字符# 至少出现一次# {1}出现一次# {3,}出现3次以上# {2,5}最少2次最多5次# \d 匹配数字# [\u4E00-\u9FA5] 匹配汉字# | 或# [] 满足任意一个 [2345] 2345中任意一个 [0-9]区间 [^1]非1# \s 空格 \S 非空格# \w 匹配[A-Za-z0-9_] \W非\wtext this is Python 数据预处理 这次学习的很好使用的环境是Anaconda4.4现在的时间是2020年1月4日# 开头 任意字符 任意次数reg_1 (^t.*)# 存在s 这样会一直匹配到最后一个sreg_2 .*(s)# 存在s 贪婪 匹配到第一个sreg_3 .*?(s)# 匹配汉字?的作用同上reg_4 .*?([\u4E00-\u9FA5]的)# 匹配日期reg_5 .*(\d{4}年)(\d{1,2}月)res re.match(reg_5, text)if res:print(res)print(res.group(2)) # group对应的数字是正则中括号的内容else:print(没匹配到)# 日期的提取print(-*20)date_text 现在的日期是2020年1月4日# date_text 现在的日期是2020年01月04日# date_text 现在的日期是2020-1-4# date_text 现在的日期是2020-01-04# date_text 现在的日期是2020/1/4# date_text 现在的日期是2020/01/04# date_text 现在的日期是2020-01reg_date .*(\d{4}[年/-]\d{1,2}[月/-]\d{,2}[日]?)res re.match(reg_date, date_text)if res:print(res)print(res.group(1)) # group对应的数字是正则中括号的内容else:print(没匹配到)# 手机号的提取print(-*20)# phone_text 我的手机号是13030010152有什么问题可以联系我# phone_text 我的手机号是17091033442有什么问题可以联系我# phone_text 我的手机号是18519299012有什么问题可以联系我phone_text 我的手机号是13691769664 有什么问题可以联系我reg_phone .*?(1[37859]\d{9})res re.match(reg_phone, phone_text)if res:print(res)print(res.group(1)) # group对应的数字是正则中括号的内容else:print(没匹配到)清洗文本数据#!/usr/bin/env python# -*- encoding: utf-8 -*-# Description 正则清洗文本数据# Time : 2020/1/4 15:52# Author : sky# Site :# File : REdealText.py# Software: PyCharmimport re# 正则对字符串的清洗def text_parse(text):# 正则过滤掉特殊符号标点英文数字等reg_1 [a-zA-Z0-9’!#$%\()*,-./:;|?—。?★、…【】《》“”‘’[\\]^_{|}~]# 去除空格reg_2 \\stext re.sub(reg_1, , text)text re.sub(reg_2, , text)# 去除换行符text text.replace(\n, )return textdef read_file(path):str_doc with open(path, encodingutf-8) as f:str_doc f.read()return str_docif __name__ __main__:# 读取文本str_doc_path ../dataset/CSCMNews/体育/0.txtstr_doc read_file(str_doc_path)print(str_doc)# 数据清洗clear_text text_parse(str_doc)print(clear_text)清洗HTML数据分析html文本信息导入正则re.I、re.L、re.M、re.S……清洗HTML标签DOCTYPE、CDATA、Script、style……HTML标签、注释、换行等处理re.compile实现正则清洗HTML数据#!/usr/bin/env python# -*- encoding: utf-8 -*-# Description 正则清洗HTML# Time : 2020/1/4 18:46# Author : sky# Site :# File : DealHtml.py# Software: PyCharmimport re# 清洗HTML标签文本def filter_tags(html_str):# 去掉多余的空格html_str .join(html_str.split())# 过滤DOCTYPEre_doctype re.compile(r*?, re.S)res re_doctype.sub(, html_str)# 过滤CDATAre_cdata re.compile(r//]∗ //\] , re.I)res re_cdata.sub(, res)# Scriptre_script re.compile(r]*[^, re.I)res re_script.sub(, res)# stylere_style re.compile(r]*[^, re.I)res re_style.sub(, res) # 去掉style# 处理换行re_br re.compile(r)res re_br.sub(, res) # 将br转换为换行# HTML标签re_h re.compile(r?\w[^]*)res re_h.sub(, res) # 去掉HTML 标签# HTML注释re_comment re.compile(r)res re_comment.sub(, res)# 多余的空行blank_line re.compile(r\n)res blank_line.sub(, res)blank_line_l re.compile(r\n)res blank_line_l.sub(, res)blank_kon re.compile(r\t)res blank_kon.sub(, res)blank_one re.compile(r\r\n)res blank_one.sub(, res)blank_two re.compile(r\r)res blank_two.sub(, res)blank_three re.compile(r )res blank_three.sub(, res)# 剔除超链接http_link re.compile(r(http://..html))res http_link.sub(, res)return resdef read_file(path):str_doc with open(path, encodingutf-8) as f:str_doc f.read()return str_docif __name__ __main__:str_doc read_file(./htmldemo.txt)res filter_tags(str_doc)print(res)简繁体转换简繁体python包下载与使用简繁对照表分析与自定义添加python实现繁体文本转为简体文本python实现简体文本转为繁体文本#!/usr/bin/env python# -*- encoding: utf-8 -*-# Description 简繁体转换# Time : 2020/1/4 19:08# Author : sky# Site :# File : zhline.py# Software: PyCharmfrom ClearText.zhtools.langconv import *# 1、简体字转化为繁体str1 上港5-4恒大5分领跑剑指冠军下轮打平便可夺冠 \武磊平纪录—广州恒大淘宝|上海上港|蔡慧康|武磊|胡尔克|张成林|阿兰|保利尼奥|王燊超|吕文君|懂球帝 \北京时间11月3日19:35中超第28轮迎来天王山之战广州恒大淘宝坐镇主场迎战上海上港。 \上半场吕文君和蔡慧康先后进球两度为上港取得领先保利尼奥和阿兰两度为恒大将比分扳平 \补时阶段保利尼奥进球反超比分下半场武磊进球追平李金羽单赛季进球纪录王燊超造成张成林乌龙 \胡尔克点射破门阿兰补时打进点球。最终上海上港客场5-4战胜广州恒大淘宝 \赛季双杀恒大同时也将积分榜上的领先优势扩大到五分上港下轮只要战平就将夺得冠军。非常抱歉line1 Converter(zh-hant).convert(str1)print(繁体---\n, line1)# 2、繁体字转化为简体str2 上港5-4恆大5分領跑劍指冠軍下輪打平便可奪冠 \武磊平紀錄—廣州恆大淘寶|上海上港|蔡慧康|武磊|胡爾克|張成林|阿蘭|保利尼奧|王燊超|呂文君|懂球帝 \北京時間11月3日19:35中超第28輪迎來天王山之戰廣州恆大淘寶坐鎮主場迎戰上海上港。 \上半場呂文君和蔡慧康先後進球兩度為上港取得領先保利尼奧和阿蘭兩度為恆大將比分扳平 \補時階段保利尼奧進球反超比分下半場武磊進球追平李金羽單賽季進球紀錄王燊超造成張成林烏龍 \胡爾克點射破門阿蘭補時打進點球。最終上海上港客場5-4戰勝廣州恆大淘寶 \賽季雙殺恆大同時也將積分榜上的領先優勢擴大到五分上港下輪只要戰平就將奪得冠軍。非常抱歉line2 Converter(zh-hans).convert(str2)print(简体---\n, line2)30万新闻数据清洗高效读取30W新闻文本实现新闻文本抽样抽样新闻文本数据清洗每万条打印一次信息实现30W新闻文本数据清洗#!/usr/bin/env python# -*- encoding: utf-8 -*-# Description 30W新闻文本数据清洗# Time : 2020/1/4 19:35# Author : sky# Site :# File : 30wClear.py# Software: PyCharmimport osimport reimport timefrom ClearText.REdealText import text_parse# 高效读取文件# 迭代器class LoadFolders(object):def __init__(self, parent_path):self.parent_path parent_pathdef __iter__(self):for file in os.listdir(self.parent_path):file_abspath os.path.join(self.parent_path, file)if os.path.isdir(file_abspath):yield file_abspathclass LoadFiles(object):def __init__(self, parent_path):self.parent_path parent_pathdef __iter__(self):folders LoadFolders(self.parent_path)# 第一级目录for folder in folders:category folder.split(os.sep)[-1]# 第二级目录for file in os.listdir(folder):file_path os.path.join(folder, file)if os.path.isfile(file_path):this_file open(file_path, rb) # rb读取快content this_file.read().decode(utf-8)yield category, contentthis_file.close()if __name__ __main__:start_time time.time()file_path ../dataset/CSCMNewsfiles LoadFiles(file_path)# 抽样n 2for index, msg in enumerate(files):if index % n 0:category msg[0]content msg[1]content text_parse(content)if int(index / n) % 10000 0:print({t} *** {i} \t docs has bean dealed.format(ttime.strftime(%Y-%m-%d %H:%M:%S, time.localtime()),iindex), \n, category, \t, content[:20])end_time time.time()print(Total Cost Time %.2f % (end_time - start_time) s)扩展缺失值处理方法(忽略/人工填值/均值/中位数等)噪声数据处理方式(向均值光滑、离群点分析等)正则学习内容均来源于学习资料在学习过程中进行记录如有侵权联系作者进行删除Change the world by program