python比较操作的内幕
今天看了序列类型相关的比较操作.
在python核心编程(2nd)一书中6.13.1章节中, 给出了列表比较的一个准则..
个人感觉还是不很完善:
如果扫描到两个列表中当前比较元素是不可比较的, 那么返回什么??
我用的是python2.6....
对这个问题做了一些测试, 自己目前嘎绝当比较遇到上述情况时, 是使用两个列表的内存地址值来比较的..
以下代码是测试时候的i/o
>>> a = [1, 2, 3, 4]
>>> b = [1, 2, 4, "3"]
>>> a == b
False
>>> a < b
True
>>> a > b
False
>>> a, b = b, a
>>> a
[1, 2, 4, '3']
>>> b
[1, 2, 3, 4]
>>> id(a)
12643064
>>> id(b)
12752576
>>> a < b
False
>>> b < a
True
>>> a = [1, "4"]
>>> b = [2]
>>> a < b
True
>>> id(a)
12642104
>>> id(b)
12643064
>>> c = [0]
>>> a < c
False
>>> id(c)
12752576
>>> a = [1]
>>> b = ["1"]
>>> a < b
True
>>> id(a)
12243072
>>> id(b)
12642104
>>> a = ["1"]
>>> b = [1]
>>> a < b
False
>>> id(a)
12643064
>>> id(b)
12243072
>>>
相关文档:
源代码下载:下载地址在这里
# 029
aFile = file(r'C:\in.txt', 'r')
while True:
line = aFile.readline()
if len(line) == 0:
break
# end of if
print line,
# end of while
aFile.close()
output:
>>>
This is the first line.
This is the second line.
This is ......
源代码下载:下载地址在这里
# 032
# 其实cPickle这个模块起到的作用可以用“完美地协调了文件中的内容(对象)和代码中的引用”来形容
import cPickle as p # 这条语句给cPickle起了个小名p
objectFileName = r'C:\Data.txt'
aList = [1, 2, 3]
f = file(objectFileName, 'w')
p.dump(aList, f)
f.close ......
源代码下载:下载地址在这里
# 033
class Person:
age = 22
def sayHello(self): # 方法与函数的区别在于需要一个self参数
print 'Hello!'
# end of def
# end of class # 这是我良好的编程风格
p = Person()
print p.age
p.sayHello()
output:
22
Hello! ......
源代码下载:下载地址在这里
# 037
fileName = ''
while 1:
fileName = raw_input("Input a file name:")
if fileName == 'q':
break
try:
f = file(fileName, 'r')
print 'Opened a file.'
except:
print 'There is no file named', fileName
......
——由于最近在做有关网页搜索的项目,涉及到一些编码方面的知识,小弟在网上偶然地发现了这么一篇文章,很易懂,不晦涩,为了方便自己也同时能方便大家,就转了过来,以作参考……
文章出处:http://blog.csdn.net/tingsking18/arc ......