价值比较

时间:2016-02-20 23:12:32

标签: python python-2.7 compare python-2.x

我想根据数值比较给出标签。但是,标签最终并不正确。

有没有人可以帮助我?

labels = []
for x,y in zip(multiply_nonspam_test, multiply_spam_test):
    if x > y:
        label = 0
        labels.append(label)
    elif x == y:
        label = 2
        labels.append(label)
    elif x < y:
        label = 1
        labels.append(label)

print labels

multiply_nonspam_test: [-0.0, -5.5014525551182665, -0.0, -0.0, -0.0, -0.0, -20.347159201740993, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -5.403260528939053, -0.0, -0.0, -9.239122173401634, -0.0, -0.0, -0.0]

multiply_spam_test: [-0.0, -4.564151631270644, -0.0, -0.0, -0.0, -0.0, -13.658061604139832, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -5.322256708105361]

标签结果(不是):

[1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1..]

2 个答案:

答案 0 :(得分:1)

我在Mac OSX上使用Python 3.5.1尝试了这个:

multiply_nonspam_test = [-0.0, -5.5014525551182665, -0.0, -0.0, -0.0, -0.0, -20.347159201740993, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -5.403260528939053, -0.0, -0.0, -9.239122173401634, -0.0, -0.0, -0.0]
multiply_spam_test = [-0.0, -4.564151631270644, -0.0, -0.0, -0.0, -0.0, -13.658061604139832, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -5.322256708105361]

labels = []
for x,y in zip(multiply_nonspam_test, multiply_spam_test):
    if x > y:
        labels.append(0)
    elif x == y:
        labels.append(2)
    elif x < y:
        labels.append(1)

print(labels)

得到了这个结果:

[2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1]

答案 1 :(得分:1)

你的程序实际上运行正常。不知何故,你必须给它错误的列表。我创建了一个小功能来显示它。

# Your original lists
list1 = [-0.0, -5.5014525551182665, -0.0, -0.0, -0.0, -0.0, -20.347159201740993, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -5.403260528939053, -0.0, -0.0, -9.239122173401634, -0.0, -0.0, -0.0]
list2 = [-0.0, -4.564151631270644, -0.0, -0.0, -0.0, -0.0, -13.658061604139832, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -5.322256708105361]


def compare_lists(list1, list2):
    labels = []
    for x, y in zip(list1, list2):
        if x > y:
            label = 0
            labels.append(label)
        elif x == y:
            label = 2
            labels.append(label)
        elif x < y:
            label = 1
            labels.append(label)

    return labels

# Output [2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1]
print compare_lists(list1, list2)
相关问题