强大的Python生成器
逐步演进
f=open('/etc/motd','r')
longest=0
while True:
lineLen=len(f.readline().strip())
if not lineLen: break
if lineLen > longest:
longest=lineLen
f.close()
return longest
问题:一直占有文件句柄未释放
f=open('/etc/motd','r')
longest=0
allLines=f.readLines()
f.close()
for line in allLines:
lineLen=len(line.strip())
if lineLen > longest:
longest=lineLen
return longest
问题:读完文件所有行再开始计算,耗费大量内存
f=open('/etc/motd','r')
allLineLens=[len(x.strip()) for x in f]
f.close()
return max(allLineLens)
问题:文件迭代器一行一行迭代f,列表解析需要文件所有行都会读到内存中
f=open('/etc/motd','r')
longest=max(len(x.strip()) for x in f)
f.close()
return longest
问题:可去掉文件打开模式,让python去处理打开的文件
return max(len(x.strip()) for x in open('etc/motd'))
这个完美了。
相关文档:
软件准备:
1.eclipse开发包,下载地址:http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/galileo/SR2/eclipse-java-galileo-SR2-win32.zip
2.pydev插件,下载地址:http://sourceforge.net/projects/pydev/files/pydev/Pydev%201.5.4/org.python.pydev.feature-1.5.4.2010011921 ......
集合类型操作符(所有的集合类型)
联合( | )
联合(union)操作和集合的OR(又称可兼析取(inclusive
disjunction))其实是等价的,两个集
合的联合是一个新集合,该集合中的每个元素都至少是其中一个集合的成员,即,属于两个集合其
中
之一的成员。联合符号有一个等价的方法,union().
Edit By Vheavens
Edit By Vhe ......
前几天,小许给我一份JavaQQ的源代码,用vim打开一看,发现里面的中文都是乱码。不用说,又是可恶的编码问题,在window下的文本文件通常使用GBK或GB18030编码,而在Linux下utf-8编码则大行其道。打开——另存为肯定不是上策,上网找编码批量转换工具也不是咱勤劳勇敢的程序员的作风。自已动手 ......
下载安装MySQLdb
http://sourceforge.net/projects/mysql-python/ 好像没看到windows版本for python2.6的下载,网上搜索到一个
http://www.technicalbard.com/files/MySQL-python-1.2.2.win32-py2.6.exe
安装后import MySQLdb会出现 DeprecationWarning: the sets module is deprecated 这样一个警告,google之
......
python string和PyQt的QString的区别 以下在Python2.6和PyQt4.4.4 for
Python2,6环境下讨论: Python中有两种有关字符的类型:Python string object和Python Unicode
object。主要使用Python string object进行数据输入输出。 PyQt中与之相对应的字符有关类
python string和PyQt的QString的区别
以下在Python2.6和PyQt4 ......