字典的排序是根据hash值来排序的,只要对象的hash值是有序的,那么结果也会是有序的.
我举个例子
import os
import random
class a( object ):
data = 0
def __init__( self ):
self.data = self.data
self.__class__.data += 1
def __hash__( self ):
return random.randint( 0 ,100 )
def __repr__( self ):
return 'a object data ---> %d' % self.data
b = dict( ( a( ) ,i ) for i in range( 6 ) )
for i in b:
print( i )
你试一下上面的代码,就会发现每次都是无序的