比较类实例和获得累积“分数” [英] Comparing class instances and attaining cumulative "score"

查看:113
本文介绍了比较类实例和获得累积“分数”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个类Person的两个实例,我想做一些比较。 sun 是每个实例的几个属性之一,由两个随机生成的数字定义。我有以下问题...正在创建一个子类,如匹配正确的方式去设置此比较?我想在这个比较结束时编译一个 overall score,我可以不用for循环吗?

  Person1 = Person(Person1)
Person2 = Person(Person2)


类匹配:

overall = 0

def __init __(self,Person1,Person2):
self.Person1 = person1
self.Person2 = Person2

def suns(self):
如果abs(Person1.sun - Person2.sun)== 2或4或8或10:
overall + = 4

elif abs(Person1.sun - Person2.sun)== 3或9:
overall - = 6

elif abs(Person1.sun - Person2.sun)== 6 :
overall + = 3

else:
overall + = 0

打印Match.overall



EDIT
澄清我想做什么...我对天文学生日图兼容性服务工作,只是为了乐趣和练习我想做一个非常基本的版本的程序运行。现在,所有的数据是随机生成的,不需要用户输入。这是Person类看起来像

 从随机导入randint 
从十进制导入十进制

def randomDecimal(a,b):
return(randint(int(a),int(100.0 * b)))/ 100.0

class Person:

def __init __(self,name):
self.name = name
self.sun =(randomDecimal(1,12),randomDecimal(0,30))
self.moon = (randomDecimal(1,12),randomDecimal(0,30))
self.mercury =(randomDecimal(1,12),randomDecimal(0,30))
self.venus = ,12),randomDecimal(0,30))
self.mars =(randomDecimal(1,12),randomDecimal(0,30))

def printer $ b print你的太阳在%f,在%f度%(self.sun [0],self.sun [1])$ ​​b $ b打印你的月亮在%f,at%f度打印机的%(self.moon [0],self.moon [1])$ ​​b $ b#等等。


解决方案

如果你想比较两个人,比较本身不是一个人。



Python类有特殊的方法可以覆盖,允许内置的python平等工作。



例如:

  ():
__init __(self,name =Sinead OConnor等):
#锅炉板在这里
self.name = name

__eq __(self,anotherPerson):
return False#Perople是奇妙和独特的,没有等价

__cmp __(self,anotherPerson):
return anotherPerson ==You没有什么比较你!

这些将允许您执行以下操作:

 > a = Person(a)
> b = Person(b)
> a == b
False

显然,如何比较人是很困难的,



对于你的工作,你需要比较抽象的人,他们是一个很好的适合sence, strong> cmp 将不会工作,因为它只是一个大于,小于或等于另一个。



在这些情况下,你可以做

  class Person():
delta = 100# match
__init __(self,name =Sinead OConnor,etc ...):
#锅炉板在这里
self.name = name

def loveOfPowerBallads (self,myRank):
if 0< myRank< 100:
raise OutOfRange#或任何
self.rankPowerBallads = myRank

def isGoodMatch(self,otherPerson) :
return(self.rankPowerBallads - other.rankPowerBallads)^ 2< Person.delta

然后:

 > ls = Person(legoStormtrooper)
> ls.loveOfPowerBallads(95)
> lp = Person(Lame Person)
> ls.loveOfPowerBallads(30)
> ls.isGoodMatch(lp)
False



编辑:使它更具体原则是一样的):



  class Person:

def __init __(self,name):
self.name = name
self.sun =(randomDecimal(1,12),randomDecimal(0,30))
self.moon =(randomDecimal(1,12),randomDecimal 0,30))
#etc ...

def比较(self,other):
overall = 0
如果abs(self.sun - other。在[3,8]中:
overall - =
total = =
6
elif abs(self.sun - other.sun)== 6:
overall + = 3
else:
overall + = 0
return overall


So, I have two instances of a class Person and I am trying to do some comparisons. sunis one of several attributes of each instance is defined by two randomly generated numbers. I have the following questions... Is creating a subclass like Match the right way to go about setting up this comparison? And I want an overall "score" compiled at the end of this comparison, can I do that without the for-loop?

Person1 = Person("Person1")
Person2 = Person("Person2")


class Match(Person):

    overall = 0

    def __init__(self, Person1, Person2):
        self.Person1 = Person1
        self.Person2 = Person2

    def suns (self): 
        if abs(Person1.sun - Person2.sun) == 2 or 4 or 8 or 10:
            overall += 4

        elif abs(Person1.sun - Person2.sun) == 3 or 9: 
            overall -= 6

        elif abs(Person1.sun - Person2.sun) == 6:
            overall += 3

        else:
            overall += 0

print Match.overall

EDIT To clarify what I am trying to do... I have some basic understanding of how the astrological birth chart compatibility services work and just for fun and practice I want to make a very basic version of that program run. For now, all the data is randomly generated and no user input is needed. Here is what the Person class looks like

from random import randint
from decimal import Decimal

def randomDecimal(a,b):
    return (randint(int(a), int(100.0 * b)))/100.0

class Person:

    def __init__(self, name):
        self.name=name
        self.sun = (randomDecimal(1, 12), randomDecimal(0, 30)) 
        self.moon = (randomDecimal(1, 12), randomDecimal(0, 30)) 
        self.mercury = (randomDecimal(1, 12), randomDecimal(0, 30))
        self.venus = (randomDecimal(1, 12), randomDecimal(0, 30))
        self.mars = (randomDecimal(1, 12), randomDecimal(0, 30))

    def printer(self):
        print "your Sun is in %f, at %f degrees" % (self.sun[0], self.sun[1])
        print "your Moon is in %f, at %f degrees" % (self.moon[0], self.moon[1])
            #and so on and so forth for the printer

解决方案

If you are trying to compare two people, the comparison itself isn't also a person.

Python classes have special methods you can overide that allow inbuilt python equalities to work.

For example:

class Person():
    __init__(self, name="Sinead OConnor", etc...):
        # boiler plate goes here
        self.name = name

    __eq__(self, anotherPerson):
        return False # Perople are wonderful and unique and have no equal

    __cmp__(self,anotherPerson):
        return anotherPerson == "You" # Nothing compares too you!

These will then allow you to do things like:

> a = Person(a)
> b = Person(b)
> a == b
False

Obviouusly, how to compare people is difficult and application specific, but once the logic is in it makes sence.

For your work, where you need to compare to people in the abstract "are they a good fit" sence, `cmp won't work as thats just is one thing greater than, less than or equal to another.

In those cases, you can do the following

class Person():
    delta = 100 # How close do 2 things need to be to be a good match
    __init__(self, name="Sinead OConnor", etc...):
        # boiler plate goes here
        self.name = name

    def loveOfPowerBallads(self,myRank):
        if 0<myRank<100:
            raise OutOfRange # Or whatever
        self.rankPowerBallads = myRank

    def isGoodMatch(self,otherPerson):
        return (self.rankPowerBallads - other.rankPowerBallads) ^ 2 < Person.delta

Then:

> ls = Person("legoStormtrooper")
> ls.loveOfPowerBallads(95)
> lp = Person("Lame Person")
> ls.loveOfPowerBallads(30)
> ls.isGoodMatch(lp)
False

Edit: Making it more specific for you (although the principle is the same):

class Person:

    def __init__(self, name):
        self.name=name
        self.sun = (randomDecimal(1, 12), randomDecimal(0, 30)) 
        self.moon = (randomDecimal(1, 12), randomDecimal(0, 30)) 
        # etc...

    def compare(self,other):
        overall = 0
        if abs(self.sun - other.sun) in [2,4,8,10]:
            overall += 4
        elif abs(self.sun - other.sun) in [3,9]: 
            overall -= 6
        elif abs(self.sun - other.sun) == 6:
            overall += 3
        else:
            overall += 0
        return overall

这篇关于比较类实例和获得累积“分数”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆