我怎样才能做自定义键函数排序,需要两个对象。我有一个实现函数的对象族,但该函数需要另一个对象族来获得结果(Score)。然后根据分数排序。参见代码class AdoptionCenter(object):
name=''
species_types={}
location=()
def __init__(self, name, species_types, location):
self.name = name
self.species_types = species_types
lst=list(location)
lst[0]=lst[0]+1.0-1.0
lst[1]=lst[1]+1.0-1.0
t = tuple(lst)
self.location = t
def get_name(self):
return self.name
def get_location(self):
return self.location
def get_species_count(self):
ss = dict(filter(lambda (a,bc): bc>0, self.species_types.items()))
#dict((k, v) for k, v in ss.items() if all(x < 5 for x in v))
return ss
def get_number_of_species(self,species_name):
return self.species_types.get(species_name,0)
def adopt_pet(self,species_name):
if ( self.species_types.get(species_name) >0):
self.species_types[species_name]=self.species_types[species_name]-1
class Adopter(object):
name=''
desired_species=''
def __init__(self, name, desired_species):
self.name=name
self.desired_species=desired_species
def get_name(self):
return self.name
def get_desired_species(self):
return self.desired_species
def get_score(self,adoption_center):
score=1.0*adoption_center.get_number_of_species(self.desired_species)
return score
class FlexibleAdopter(Adopter,object):
considered_species=[]
def __init__(self, name, desired_species, considered_species):
super(FlexibleAdopter,self).__init__(name, desired_species)
self.considered_species=considered_species
def get_score(self,adoption_center):
s1=super(FlexibleAdopter, self).get_score(adoption_center)
s2=0
for k in self.considered_species:
xx=adoption_center.get_number_of_species(k)
if xx is None:
xx=0
s2=s2+0.3*xx
scr=s1+s2
return scr
class FearfulAdopter(Adopter,object):
feared_species =''
def __init__(self, name, desired_species, feared_species):
super(FearfulAdopter,self).__init__(name, desired_species)
self.feared_species=feared_species
def get_score(self,adoption_center):
s1=super(FearfulAdopter, self).get_score(adoption_center)
s2=adoption_center.get_number_of_species(self.feared_species)
if s2 is None:
s2=0
scr=s1-0.3*s2
if scr <0:
return 0.0
else:
return scr
adopter = MedicatedAllergicAdopter("One", "Cat", ['Dog', 'Horse'], {"Dog": .5, "Horse": 0.2})
adopter2 = Adopter("Two", "Cat")
adopter3 = FlexibleAdopter("Three", "Horse", ["Lizard", "Cat"])
adopter4 = FearfulAdopter("Four","Cat","Dog")
adopter5 = SluggishAdopter("Five","Cat", (1,2))
adopter6 = AllergicAdopter("Six", "Cat", "Dog")
ac = AdoptionCenter("Place1", {"Mouse": 12, "Dog": 2}, (1,1))
ac2 = AdoptionCenter("Place2", {"Cat": 12, "Lizard": 2}, (3,5))
ac3 = AdoptionCenter("Place3", {"Horse": 25, "Dog": 9}, (-2,10))
def get_mkey(Adopter):
return Adopter.get_score()
lst1=[adopter, adopter2, adopter3, adopter4, adopter5, adopter6]
sorted(lst1, key=Adopter.get_score(ac))
# how to test get_adopters_for_advertisement
get_adopters_for_advertisement(ac, [adopter, adopter2, adopter3, adopter4, adopter5, adopter6], 10)
# you can print the name and score of each item in the list returned
adopter4 = FearfulAdopter("Four","Cat","Dog")
adopter5 = SluggishAdopter("Five","Cat", (1,2))
adopter6 = AllergicAdopter("Six", "Lizard", "Cat")
ac = AdoptionCenter("Place1", {"Cat": 12, "Dog": 2}, (1,1))
ac2 = AdoptionCenter("Place2", {"Cat": 12, "Lizard": 2}, (3,5))
ac3 = AdoptionCenter("Place3", {"Cat": 40, "Dog": 4}, (-2,10))
ac4 = AdoptionCenter("Place4", {"Cat": 33, "Horse": 5}, (-3,0))
ac5 = AdoptionCenter("Place5", {"Cat": 45, "Lizard": 2}, (8,-2))
ac6 = AdoptionCenter("Place6", {"Cat": 23, "Dog": 7, "Horse": 5}, (-10,10))
# how to test get_ordered_adoption_center_list
get_ordered_adoption_center_list(adopter4, [ac,ac2,ac3,ac4,ac5,ac6])
# you can print the name and score of each item in the list returned