易截截图软件、单文件、免安装、纯绿色、仅160KB

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


相关文档:

Python入门的36个例子 之 23

源代码下载:下载地址在这里
# 026
aList = ['1','2','3','4']
aListCopy = aList # 其实,这里仅仅复制了一个引用
del aList[0]
print aList
print aListCopy # 两个引用指向了了同一个对象,所以打印结果一样
aListCopy = aList[:] # 这是复制整个对象的有效方法
del aList[0]
print aList
print aListCopy
......

Python入门的36个例子 之 24

# 027
toolName = 'Google'
if toolName.startswith('Go'):
print 'The tool\'s name starts with \"Go\".'
if 'oo' in toolName:
print 'The tool\'s name contains the stirng "oo".'
print toolName.find('gl') # 返回首次出现“gl”字符串的索引
if toolName.find('Baidu ......

Python入门的36个例子 之 32

源代码下载:下载地址在这里
A Byte Of Python
中关于继承有这样的文字:
Suppose you want to write a program which has to keep track of the
teachers and students in a college. They have some common
characteristics such as name, age and address. They also have specific
characteristics such as sala ......

Python入门的36个例子 之 33

源代码下载:下载地址在这里
# 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
......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号