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

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. COJ 1011 WZJ的数据结构(十一)树上k大

    题解:主席树&DFS序. PS:为什么我一开始Wa了N发 是因为有一个左区间我写成[L,M+1]了.......................... #include<iostream ...

  2. Word Break II——LeetCode

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  3. [Design Pattern] Service Locator Pattern 简单案例

    Service Locator Pattern,即服务定位模式,用于定位不同的服务.考虑到 InitialContext::lookup 的成本比较高,提供了 Cache 类缓存以定位到的服务. 代码 ...

  4. Topcoder 好题推荐

    SRM SRM147 DIV1 1000pt DP SRM148 DIV1 1100pt 递归 SRM149 DIV1 1000pt math SRM150 DIV1 500pt DP SRM469 ...

  5. 当多个客户请求一个servlet时,引擎为每个客户启动一个线程,那么servlet类的成员变量被所有的线程共享?

    因为servlet的实现是单例,多线程也就是说,N个客户端请求同一个servlet,他们所请求的是同一个对象,成员变量是属于这个对象的,因此成员变量也被共享了因此在servlet编程中,无状态的ser ...

  6. vs2008打包公布程序

    vs2008打包公布程序 一vs2008中新建 安装项目,确定(新建 安装向导 也能够) 二.加入内容 1.加入主要内容: 应用程序目录=>右键=>加入=>文件,找到须要的文件,包含 ...

  7. [AngularJS] angular-md-table for Angular material design

    Download from npm:https://www.npmjs.com/package/angular-md-table +: Responsive: Has both Mobile view ...

  8. [AngularJS] Using $parse Service

    $parse is useful when you want to parse an expression and the context is not defined yet. For exampl ...

  9. Java基础知识强化75:正则表达式之分割功能(字符串中的数字排序案例)

    1. 案例分析: 我有如下一个字符串:"91 27 46 38 50" 写代码实现最终输出结果是:"27 38 46 50 91" 分析:    (1)定义一个 ...

  10. 轮播图--JS手写

    轮播图基本每个网站都会有,也有很多的JQuery插件可以用,这里是用JS代码写的. @{ Layout = null; } <!DOCTYPE html> <html> < ...