Python:比较三个列表,而不是两次存储相同的数字

时间:2016-12-12 23:11:28

标签: python for-loop append break enumerate

我有三个清单:

    list1 = [10, 20, 30]
    list2 = [22, 19, 32]
    list3 = [18, 21, 28]

我想过滤每个列表,以便在其他两个列表中删除不在5位数内的数字到另一个数字。但是,我不希望任何号码被使用两次。这是我的代码:

    output1 = []
    output2 = []
    output3 = []

    for i, valuei in enumerate(list1):
        for j, valuej in enumerate(list2):
            for k, valuek in enumerate(list3):
                delta1_2 = list1[i] - list2[j]
                delta2_3 = list2[j] - list3[k]
                if abs(delta1_2) < 5 and abs(delta2_3) < 5:
                    output1.append(valuei)
                    output2.append(valuej)
                    output3.append(valuek)
                    k=k+1
                    break
                else:
                    continue
    print(output1, output2, output3)

但是,我的输出如下:

  

[20,20,30] [22,19,32] [18,18,28]

我想让它像这样出来(我希望&#34; 20&#34;只能使用一次,然后转到#34; 30&#34;在list1中):

  

[20,30] [22,32] [18,28]

3 个答案:

答案 0 :(得分:0)

使用'set'功能:

    output1 = []
output2 = []
output3 = []

for i, valuei in enumerate(list1):
    for j, valuej in enumerate(list2):
        for k, valuek in enumerate(list3):
            delta1_2 = list1[i] - list2[j]
            delta2_3 = list2[j] - list3[k]
            if abs(delta1_2) < 5 and abs(delta2_3) < 5:
                output1.append(valuei)
                output2.append(valuej)
                output3.append(valuek)
                k=k+1
                break
            else:
                continue
print(set(output1), set(output2), set(output3))

答案 1 :(得分:0)

您所询问的内容与您的示例不符...

def within(x, n, ys):
    return any(abs(x - y) < n for y in ys)

def filter(list_in, list_to_compare):
    return [x for x in list_in if within(x, 5, list_to_compare)]

您的输入:

list1 = [10, 20, 30]
list2 = [22, 19, 32]
list3 = [18, 21, 28]

print(filter(list1, list2 + list3))
print(filter(list2, list1 + list3))
print(filter(list3, list1 + list2))

打印:

[20, 30]
[22, 19, 32]
[18, 21, 28]

19和21与list1中的20之间的距离是5,因此不应过滤掉它们。

答案 2 :(得分:0)

非常感谢我的朋友解决这个问题。

    list1 = [10, 20, 30]
    list2 = [22, 19, 32]
    list3 = [18, 21, 28]

    output1 = []
    output2 = []
    output3 = []

    keepGoing = True
    matchFound = False
    while(keepGoing):
            matchFound = False
            for i, valuei in enumerate(list1):
                if(matchFound):
                    break
                else:
                    for j, valuej in enumerate(list2):
                            if(matchFound):
                                    break
                            else:
                                    for k, valuek in enumerate(list3):
                                            delta1_2 = list1[i] - list2[j]
                                            delta2_3 = list2[j] - list3[k]
                                            if abs(delta1_2) < 5 and abs(delta2_3) < 5:
                                                    matchFound = True
                                                    output1.append(list1[i])
                                                    output2.append(list2[j])
                                                    output3.append(list3[k])
                                                    list1 = list1[i+1:]
                                                    list2 = list2[j+1:]
                                                    list3 = list3[k+1:]
                                                    if not list1 or not list2 or not list3:
                                                            keepGoing = False
                                                    break

    print((output1), (output2), (output3))

输出:

  

[20,30] [22,32] [18,28]