以下是自己编程的一些小贴士,记录,总结提高自己。

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集锦的更多相关文章

  1. 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 ...

  2. Java编程Tips

    原文: Java编程中"为了性能"尽量要做到的一些地方 作者: javatgo 最近的机器内存又爆满了,除了新增机器内存外,还应该好好review一下我们的代码,有很多代码编写过于 ...

  3. 【转】高效Java编程工具集锦

    原文地址:http://geek.csdn.net/news/detail/57469 Java 开发者常常都会想办法如何更快地编写 Java 代码,让编程变得更加轻松.目前,市面上涌现出越来越多的高 ...

  4. 前端编程tips

    1.ts less 网上搜视频教程,不用太复杂的,短短几分钟视频基本就对其入门了,比自己搜官网学习更方便. 常用的ts技术:let name:string="";  let obj ...

  5. Spring入门编程问题集锦Top10

    我写的一篇文章,希望对spring初学者有所帮助: 1.如何学习Spring? 你可以通过下列途径学习spring: ①. spring下载包中doc目录下的MVC-step-by-step和samp ...

  6. [C++]高效C/C ++编程tips

    Effective C++ 视C++ 为一个语言联邦(C.Object-Oriented C++.Template C++.STL) 宁可以编译器替换预处理器(尽量以const.enum.inline ...

  7. 编程Tips

    三元运算符 Vb中的iif(expr,truepart,falsepart)和C#中的expr?truepart:falsepart. 无论expr的结果是true还是false,true/false ...

  8. stm8编程tips(stvd)

    编译完成时显示程序占用的flash和ram大小 将附件压缩包中的mapinfo.exe解压到stvd的安装路径\stvd中 在工程上点右键选settings 右侧的选项卡选择Linker,将categ ...

  9. vim tips 集锦

    删除文件中的空行 :g/^$/d g 表示 global,全文件 ^ 是行开始,$ 是行结束 d 表示删除该 这里只能匹配到没有白空符的空行,假如要删除有空白符的空行,则使用: :g/^\s*$/d ...

随机推荐

  1. -_-#微信内置JavaScript API WeixinJSBridge

    微信相关的 js 操作:分享.网络.菜单 微信内置JsAPI之WeixinJSBridge微信WeixinJSBridge API续

  2. 设计模式(六):Singleton 单件模式 -- 创建型模式

    1.定义 当需要控制一个类的实例数量且调用者可以从一个公共的访问点访问时. 2.适用场景 1. 当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时. 2. 当这个唯一实例应该是通过子类化可扩 ...

  3. Hadoop FileInputFormat实现原理及源码分析

    FileInputFormat(org.apache.hadoop.mapreduce.lib.input.FileInputFormat)是专门针对文件类型的数据源而设计的,也是一个抽象类,它提供两 ...

  4. win7配置iis 出现:HTTP 错误 403.14 - Forbidden

    打开 IIS 管理器. 在“功能”视图中,双击“目录浏览”. 在“目录浏览”页上,在“操作”窗格中单击“启用”. 确认站点或应用程序配置文件中的 configuration/system.webSer ...

  5. SRM593(1-250pt,500pt)

    SRM 593 DIV1 250pt 题意:有如下图所示的平面,每个六边形有坐标.将其中一些六边形染色,要求有边相邻的两个六边形不能染同一种颜色.给定哪些六边形需要染色,问最少需要多少种颜色. 解法: ...

  6. Java类加载器深入理解

    本篇文章主要是详细写一下个人对Java ClassLoader的理解. 首先回顾一下,java虚拟机载入java类的步骤:java文件经过编译器编译后变成字节码文件(.class文件),类加载器(Cl ...

  7. 点击按钮弹出div,留用

    <input type="button" onclick="document.getElementById('div').style.display=(docume ...

  8. SKShapeNode类

    继承自 SKNode:UIResponder:NSObject 符合 NSCoding(SKNode)NSCopying(SKNode)NSObject(NSObject) 框架  /System/L ...

  9. 机房管理系统——vb与excel链接2

    因为我之前找的文档让我以为在创建表里面走了非常大的误区,所以当时我直接就在学生管理系统的目录里建了张表,执行时候直接打开这样表即可了. 可是这里面还是存在着非常大的误区. 后来我看了周坤的博客感觉他比 ...

  10. honeywell D6110开发的一个工厂仓库追溯识别

    近日.接触并开发了一个用honeywell D6110 二维扫描PDA的项目,应用也比較简单. 就是货品物料编码.通过中间码相应,然后中间码再依照不同OEM品牌须要生成各种商品条码并带有流水号. 要求 ...