列表中的python比较,包括数值和分类值

时间:2017-12-03 18:33:25

标签: python string list if-statement

我正在尝试比较列表中的某些值,它可能是数值或分类值。这是我的代码:

        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]

如果row [feature]有一个数值,我想做大于或小于比较。如果它是一个字符串,我想做同等或不等比较。

例如,如果row [feature] = 0且此[feature] [0] = 1,则应打印“小于”。如果row [feature] ='f'且此[feature] [0] ='t',则应打印“diff string”。目前,在这种情况下,我的代码仍会打印“小于”。我在第一个if语句中做错了吗?或者我怎样才能打印“diff string”?

0 个答案:

没有答案