编程Tips集锦
以下是自己编程的一些小贴士,记录,总结提高自己。
1.python中集合类型的查找,尽量用dict or set类型。
dict和set类型,在python内部的实现都是使用hash映射,查找的时间复杂度是O(1),比任何的查找算法都高效。
当在程序中使用到>1K次的查询,就应该开始考虑使用dict或set类型来进行数据的组织。
#coding:utf-8
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import string
import operator
import datetime commonWords = ["the", "be", "and", "of", "a", "in", "to", "have", "it", "i", "that", "for", "you", "he", "with", "on", "do", "say", "this", "they", "is", "an", "at", "but","we", "his", "from", "that", "not", "by", "she", "or", "as", "what", "go", "their","can", "who", "get", "if", "would", "her", "all", "my", "make", "about", "know", "will","as", "up", "one", "time", "has", "been", "there", "year", "so", "think", "when", "which", "them", "some", "me", "people", "take", "out", "into", "just", "see", "him", "your", "come", "could", "now", "than", "like", "other", "how", "then", "its", "our", "two", "more", "these", "want", "way", "look", "first", "also", "new", "because", "day", "more", "use", "no", "man", "find", "here", "thing", "give", "many", "well"]
#若不注释,则为set类型,跑一遍程序,对比一下,则知优劣!
#commonWords = set(commonWords) def isCommon(word):
global commonWords
if word in commonWords:
return True
return False def cleanText(input):
input = re.sub('\n+', " ", input).lower()
input = re.sub('\[[0-9]*\]', "", input)
input = re.sub(' +', " ", input)
input = re.sub("u\.s\.", "us", input)
input = bytes(input, "UTF-8")
input = input.decode("ascii", "ignore")
return input def cleanInput(input):
input = cleanText(input)
cleanInput = []
input = input.split(' ')
for item in input:
item = item.strip(string.punctuation)
if len(item) > 1 or (item.lower() == 'a' or item.lower() == 'i'):
cleanInput.append(item) cleanContent = []
for word in cleanInput:
if not isCommon(word):
cleanContent.append(word)
return cleanContent def getNgrams(input, n):
input = cleanInput(input)
output = {}
for i in range(len(input)-n+1):
ngramTemp = " ".join(input[i:i+n])
if ngramTemp not in output:
output[ngramTemp] = 0
output[ngramTemp] += 1
return output def getFirstSentenceContaining(ngram, content):
#print(ngram)
sentences = content.split(".")
for sentence in sentences:
if ngram in sentence:
return sentence
return "" content = str(urlopen("http://pythonscraping.com/files/inaugurationSpeech.txt").read(), 'utf-8') print('Use the set as the format of common words.')
print('Begin:',datetime.datetime.now())
for i in range(50):
ngrams = getNgrams(content, 2)
sortedNGrams = sorted(ngrams.items(), key = operator.itemgetter(1), reverse = True)
print('End:',datetime.datetime.now())
print(sortedNGrams)
2.python往数据库插入数据
在插入数据之前,记得先进行一次查询,查看数据是否已经在数据库中。
一可以使程序更健壮,二也可顺便避免二次查询。
3.数据库在建表的时候,最后有索引
最近需要往数据库中插入上百万级的数据,十万级以后之后,数据库变得极慢,磁盘读写也是爆满!
后来,发现查询次数太多,重新建表,顺便加入索引。特别是unique index,我猜背后的实现机制是hash映射。
加入索引之后的数据库,大大减轻了磁盘的负担,查询速度几乎恒定,不过数据库的增大还是降低了读写的速度(实属情理之中)。
3.python字符串中转义字符的处理
python中\t所占位为4位,不是通常的8位。
编程Tips集锦的更多相关文章
- Spring MVC 学习笔记1 - First Helloworld by Eclipse【& - java web 开发Tips集锦】
Spring MVC 学习笔记1 - First Helloworld by Eclipse reference:http://www.gontu.org 1. 下载 Spring freamwork ...
- Java编程Tips
原文: Java编程中"为了性能"尽量要做到的一些地方 作者: javatgo 最近的机器内存又爆满了,除了新增机器内存外,还应该好好review一下我们的代码,有很多代码编写过于 ...
- 【转】高效Java编程工具集锦
原文地址:http://geek.csdn.net/news/detail/57469 Java 开发者常常都会想办法如何更快地编写 Java 代码,让编程变得更加轻松.目前,市面上涌现出越来越多的高 ...
- 前端编程tips
1.ts less 网上搜视频教程,不用太复杂的,短短几分钟视频基本就对其入门了,比自己搜官网学习更方便. 常用的ts技术:let name:string=""; let obj ...
- Spring入门编程问题集锦Top10
我写的一篇文章,希望对spring初学者有所帮助: 1.如何学习Spring? 你可以通过下列途径学习spring: ①. spring下载包中doc目录下的MVC-step-by-step和samp ...
- [C++]高效C/C ++编程tips
Effective C++ 视C++ 为一个语言联邦(C.Object-Oriented C++.Template C++.STL) 宁可以编译器替换预处理器(尽量以const.enum.inline ...
- 编程Tips
三元运算符 Vb中的iif(expr,truepart,falsepart)和C#中的expr?truepart:falsepart. 无论expr的结果是true还是false,true/false ...
- stm8编程tips(stvd)
编译完成时显示程序占用的flash和ram大小 将附件压缩包中的mapinfo.exe解压到stvd的安装路径\stvd中 在工程上点右键选settings 右侧的选项卡选择Linker,将categ ...
- vim tips 集锦
删除文件中的空行 :g/^$/d g 表示 global,全文件 ^ 是行开始,$ 是行结束 d 表示删除该 这里只能匹配到没有白空符的空行,假如要删除有空白符的空行,则使用: :g/^\s*$/d ...
随机推荐
- -_-#【Canvas】
context.lineWidth = 0.5 incorrect display lineWidth=1 at html5 canvas canvas.save() canvas.restore() ...
- Perl时间处理函数
官方网址:http://search.cpan.org/~stbey/Date-Calc-6.3/lib/Date/Calc.pod#___top use Date::Calc qw( Days_in ...
- SVN linux端配置
1.create a folder: mkdir /sandbox/svn 2.create svn repository: svnadmin create /sandbox/svn/ ...
- B - Dining - poj 3281(最大流)
题目大意:有一群牛,还有一些牛喜欢的食物和喜欢的饮料,不过这些牛都很特别,他们不会与别的牛吃同一种食物或者饮料,现在约翰拿了一些食物和饮料,同时他也知道这些牛喜欢的食物和饮料的种类,求出来最多能让多少 ...
- java笔记14之private
private: 1 是一个权限修饰符 2 可以修饰成员变量和成员方法 被其修饰的成员只能在本类中被访问 class Demo { //int num = 1 ...
- 转:jQuery LigerUI 使用教程表格篇(3) 复选框、多表头、分组、汇总和明细
阅读目录 复选框 多表头 分组 汇总 明细 复选框 grid可以设置复选框模式进行多选,只需要简单的配置 checked:true 获取选中行 如果要获取选中的行,可以用getSelecteds方法: ...
- SpriteKit游戏开发
http://blog.csdn.net/larrysai/article/category/1663301 http://blog.csdn.net/ping_yun_long/article/de ...
- NYOJ 214 最长上升子序列nlogn
普通的思路是O(n2)的复杂度,这个题的数据量太大,超时,这时候就得用nlogn的复杂度的算法来做,这个算法的主要思想是只保存有效的序列,即最大递增子序列,然后最后得到数组的长度就是最大子序列.比如序 ...
- python复制--笔记
对象引用: >>> songs = ["Bee","Core","Love"] >>> bat = so ...
- C#判断网站运行状态是否正常
我使用的是控制台应用程序来监控网站的运行状态,通过判断网站请求头(HEAD)来判断是否运行正常 下面列出几种常见的网站状态码 StatusCode 数字表示 OK 200. OK 指示请求成功,且请求 ...