dimanche 3 décembre 2017

python comparison in list with both numerical and categorical values

I'm trying to compare some values in a list and it could be either numerical value or categorical value. Here's my code:

        if type(row[feature]) == int or type(row[feature] == float):
            if row[feature] <= this[feature][0]:
                this = this[feature][1]
                print("less than")
            else:
                this = this[feature][2]
                print("greater than")
        else:
            if row[feature] == this[feature][0]:
                print("same string")
                this = this[feature][1]
            else:
                print("diff string")
                this = this[feature][2]

If row[feature] has a numerical value, I want to do the greater than or less than comparison. And if it is a string, I want to do the equal to or not comparison.

For example, if row[feature] = 0 and this[feature][0] = 1, it should print "less than". And if row[feature] = 'f' and this[feature][0] = 't', it should print "diff string". Currently my code will still prints "less than" in this case. Did I do something wrong in the first if statement? Or how can I make it print "diff string"?

Aucun commentaire:

Enregistrer un commentaire