简体   繁体   English

比较类实例并获得累积的“分数”

[英]Comparing class instances and attaining cumulative “score”

So, I have two instances of a class Person and I am trying to do some comparisons. 因此,我有一个类Person的两个实例,并且试图进行一些比较。 sun is one of several attributes of each instance is defined by two randomly generated numbers. sun是每个实例的几个属性之一,由两个随机生成的数字定义。 I have the following questions... Is creating a subclass like Match the right way to go about setting up this comparison? 我有以下问题...创建像Match这样的子类是否是进行此比较的正确方法? And I want an overall "score" compiled at the end of this comparison, can I do that without the for-loop? 而且我希望在比较结束时编译一个overall “分数”,如果没有for循环,我可以这样做吗?

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 这是Person类的样子

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. Python类具有可以覆盖的特殊方法,这些方法允许内置的python等式工作。

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. 在您的工作中,您需要与抽象的“适合他们的情况”下的人进行比较,` cmp不会起作用,因为那仅仅是一件事大于,小于或等于另一件事。

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

In Python, this line 在Python中,这一行

if abs(self.sun - other.sun) == 2 or 4 or 8 or 10:

actually means 实际上意味着

if (abs(self.sun - other.sun) == 2) or 4 or 8 or 10:

Any non-empty value is regarded as True . 任何非空值都被视为True so this expression will be always True . 因此,此表达式将始终为True The proper form 适当的形式

if abs(self.sun - other.sun) in (2, 4, 8, 10):

The same goes fp the second if too 如果也是如此,第二个也是

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM