Python 2.7 python元组比较&x27;小于';重温(2.7)

Python 2.7 python元组比较&x27;小于';重温(2.7),python-2.7,Python 2.7,我想了解Python对元组比较运算符的行为,特别是

我想了解Python对元组比较运算符的行为,特别是():错 ---单项元组,等值:直观,无意外--- (1,)==(1,):真 (1,)<(1,):假 (1,)>(1,):错误 ---单品差异:直观,无意外--- (1,)==(2,):False (1,)<(2,):正确 (1,)>(2,):错误 ---双项相同:直观,无意外--- (1,2)==(1,2):真 (1,2)<(1,2):错误 (1,2)>(1,2):错误 *问题:ab是否都产生错误 *因为Python在上短路了 *a[0]b[0])? *Python从不费心比较秒 *元素:a[1]b[1])。 ---双项目第一个=相同的第二个=差异:???(1,3)是什么意思:错 *问题:这里我得到了==比较,它产生了我预期的False。 *但这种比较是怎么回事? *即使将a[0] 在上面最后一个“*问题”的基础上,按字典顺序比较a[0]就像比较'1',这仍然应该是错误的,是吗

tcomp.py:

import platform

def tupleInfo( a, b, msg ):
   # using labels instead of eval-style stuff to keep things simpler.
   print
   print '--- ' , msg , ' ---'
   print a, ' == ', b, ' : ', a == b
   print a, '  < ', b, ' : ', a  < b
   print a, '  > ', b, ' : ', a  > b

print 'tcomp.py'
print 'version=', platform.python_version()

# let's start with some empty tuples.
e1 = tuple()
e2 = tuple()
tupleInfo( tuple( )         , tuple( )         , 'empty tuples,intuitive, no surprises' )
tupleInfo( tuple( [ 1 ] )   , tuple( [ 1 ] )   , 'single-item tuples, equal values: intuitive, no surprises' )
tupleInfo( tuple( [ 1 ] )   , tuple( [ 2 ] )   , 'single-item diff: intuitive, no surprises' )
tupleInfo( tuple( [ 1, 2 ] ), tuple( [ 1, 2 ] ), 'double-item same: intuitive, no surprises' )

print '* Question: do a<b and a>b both yield False '
print '* because Python short circuits on'
print '* a[0] < b[0]    (and correspondingly a[0] > b[0] )?'
print '* e.g. Python never bothers comparing second'
print '* elements: a[1] < b[1]  (and, correspondinlgy, a[1] > b[1] ).'


tupleInfo( tuple( [ 1, 2 ] ), tuple( [ 1, 3 ] ), 'double-item 1st=same 2nd=diff: ??? What is with a<b ???' )

print '* Question: Here I get the == comparison, that yields False like I would expect.'
print '* But WHAT is going on with the < comparison?'
print '* Even comapring "lexicographically", how does a[0] < b[0]'
print '* actually yield True ?'
print '* Is Python really  comparing a[0] < b[0] ?'
print '* Because in my mental model that is the same as comparing: 1 < 1'
print '* I kind of think 1 < 1 is supposed to be False, even if Python'
print '* is comparing "1" < "1"  (e.g. lexicographically).'
导入平台
def tupleInfo(a、b、msg):
#使用标签而不是eval样式的东西使事情更简单。
打印
打印'--',消息'--'
打印a,'=',b,':',a==b
打印a,“<”,b,“:”,a',b':',a>b
打印“tcomp.py”
打印“version=”,platform.python_version()
#让我们从一些空元组开始。
e1=元组()
e2=元组()
tupleInfo(tuple(),tuple(),‘空tuple,直观,没有意外’)
tupleInfo(tuple([1]),tuple([1])
import platform

def tupleInfo( a, b, msg ):
   # using labels instead of eval-style stuff to keep things simpler.
   print
   print '--- ' , msg , ' ---'
   print a, ' == ', b, ' : ', a == b
   print a, '  < ', b, ' : ', a  < b
   print a, '  > ', b, ' : ', a  > b

print 'tcomp.py'
print 'version=', platform.python_version()

# let's start with some empty tuples.
e1 = tuple()
e2 = tuple()
tupleInfo( tuple( )         , tuple( )         , 'empty tuples,intuitive, no surprises' )
tupleInfo( tuple( [ 1 ] )   , tuple( [ 1 ] )   , 'single-item tuples, equal values: intuitive, no surprises' )
tupleInfo( tuple( [ 1 ] )   , tuple( [ 2 ] )   , 'single-item diff: intuitive, no surprises' )
tupleInfo( tuple( [ 1, 2 ] ), tuple( [ 1, 2 ] ), 'double-item same: intuitive, no surprises' )

print '* Question: do a<b and a>b both yield False '
print '* because Python short circuits on'
print '* a[0] < b[0]    (and correspondingly a[0] > b[0] )?'
print '* e.g. Python never bothers comparing second'
print '* elements: a[1] < b[1]  (and, correspondinlgy, a[1] > b[1] ).'


tupleInfo( tuple( [ 1, 2 ] ), tuple( [ 1, 3 ] ), 'double-item 1st=same 2nd=diff: ??? What is with a<b ???' )

print '* Question: Here I get the == comparison, that yields False like I would expect.'
print '* But WHAT is going on with the < comparison?'
print '* Even comapring "lexicographically", how does a[0] < b[0]'
print '* actually yield True ?'
print '* Is Python really  comparing a[0] < b[0] ?'
print '* Because in my mental model that is the same as comparing: 1 < 1'
print '* I kind of think 1 < 1 is supposed to be False, even if Python'
print '* is comparing "1" < "1"  (e.g. lexicographically).'
def isSmaller(a, b): # returns true if a<b, false if a>=b
    for i in xrange(0, a.__len__()): # for every elementof a, starting from left
        if b.__len__() <= i: # if a starts with b, but a is longer, eg. b='ab', a='ab...'
            return False
        if a[i] < b[i]: # if a starts like b, but there is difference on i-th position, 
                        # eg. a='abb...', b='abc...', 
            return True
        if a[i] > b[i]: # eg. a='abc...', b='abb...', 
            return False
        if a[i] == b[i]: # if there is no difference, check the next position
            pass
    if a.__len__() < b.__len__(): # if b starts with a, but b is longer, eg. a='ac', b='ac...'
        return True
    else: # else, ie. when a is the same as b, eg. a='acdc', b='acdc'
        return False


print (1,2,3)<(1,2)
print (1,2,3)<(1,2,3)
print (1,2,3)<(1,3,2)
print isSmaller((1,2,3),(1,2))
print isSmaller((1,2,3),(1,2,3))
print isSmaller((1,2,3),(1,3,2))
False
False
True
False
False
True