0

我对 Python 很陌生,所以请善待!:) 我现在只是在搞乱,但我找不到关于这个特定问题的任何具体内容。

我希望根据两个数字的总和从输出中删除重复项。我有以下数字列表和短代码,我可以在其中打印乘以得到 24 的数字。

A = 1,2,3,4,5,6,8
B = 1,2,3,4,5,6,8

for a in A:
    for b in B:
        if (a * b) == 24:
            print (a, b, a+b)

这里的结果是:

3 8 11
4 6 10
6 4 10
8 3 11

在输出中,有四种组合给出 24,其中两种是彼此的排列。有什么方法可以对排列进行重复数据删除以获得“唯一”输出?我认为这样做的一种方法是a+b根据这个数字添加和尝试删除输出中的重复项,但我想不出一种方法来做到这一点。有人知道我该怎么做吗?

4

3 回答 3

0

您可以创建跟踪结果为 24 的数字,并可以避免重复数字如下

A = 1,2,3,4,5,6,8
B = 1,2,3,4,5,6,8
pass_list: list = []

for a in A:
    for b in B:
        if (a * b) == 24 and b not in pass_list:
            pass_list.append(a)
            print (a, b, a+b)

输出

3 8 11
4 6 10

只是将@patrick 的比较与 1000000 个计数进行比较,这个答案似乎更快一些。

Olivn: 9.081725899999999
Patrick:  7.241247899999999
this:  5.5779484

Olivn: 9.3896246
Patrick:  7.960243800000001
this:  5.6804922

Olivn: 10.6548058
Patrick:  8.282727000000001
this:  7.1877061

Olivn: 11.1385039
Patrick:  9.226674899999999
this:  8.4106494

A = 1,2,3,4,5,6,8 
B = 1,2,3,4,5,6,8 

def ol():
    res = set()
    for a in A:
        for b in B:
            if a * b == 24:
                if {a, b} not in res:
                    # print(a, b, a + b)
                    res.add(frozenset((a, b)))

def pa():
    got = set() # collect (a,b) and (b,a) that you printed already to avoid dupes
    for a in A:
        for b in B:
            if (a * b) == 24:
                if (a,b) in got:
                    continue
                got.add((a,b))  # need to add (a,b) and (b,a) to avoid printing 
                got.add((b,a))  # (b,a) after you already printed (a,b)
                # print (a, b, a+b)
 

def pr():
    pass_list: list = []

    for a in A:
        for b in B:
            if (a * b) == 24 and b not in pass_list:
                pass_list.append(a)
                #print (a, b, a+b)


from timeit import timeit

print("Olivn:", timeit(ol, number=1000000))
print("Patrick: ", timeit(pa, number=1000000))
print("this: ", timeit(pr, number=1000000))
于 2020-10-17T09:30:40.943 回答
0

@PatrickArtner 决定在他的回答下的评论中不遵循我的建议,所以这是正确的(恕我直言)如何申请set frozenset以及)

A = 1,2,3,4,5,6,8
B = 1,2,3,4,5,6,8

res = set()
for a in A:
    for b in B:
        if a * b == 24:
            if {a, b} not in res:
                print(a, b, a + b)
                res.add(frozenset((a, b)))
于 2020-10-17T09:42:23.333 回答
0

如果打印,创建一个集合并存储(a,b),的组合。如果已经在集合中,(b,a)请不要打印:(a,b)

A = 1,2,3,4,5,6,8
B = 1,2,3,4,5,6,8

got = set() # collect (a,b) and (b,a) that you printed already to avoid dupes
for a in A:
    for b in B:
        if (a * b) == 24:
            if (a,b) in got:
                continue
            got.add((a,b))  # need to add (a,b) and (b,a) to avoid printing 
            got.add((b,a))  # (b,a) after you already printed (a,b)
            print (a, b, a+b)

输出:

3 8 11
4 6 10

让我们比较一下这个和olivn的方法 - 我删除了两个打印语句:

A = 1,2,3,4,5,6,8 
B = 1,2,3,4,5,6,8 

def ol():
    res = set()
    for a in A:
        for b in B:
            if a * b == 24:
                if {a, b} not in res:
                    # print(a, b, a + b)
                    res.add(frozenset((a, b)))

def pa():
    got = set() # collect (a,b) and (b,a) that you printed already to avoid dupes
    for a in A:
        for b in B:
            if (a * b) == 24:
                if (a,b) in got:
                    continue
                got.add((a,b))  # need to add (a,b) and (b,a) to avoid printing 
                got.add((b,a))  # (b,a) after you already printed (a,b)
                # print (a, b, a+b)
 

from timeit import timeit

print("Olivn:", timeit(ol, number=10000))
print("this: ", timeit(pa, number=10000))

输出:

Olivn: 0.60943335
this:  0.6066992629999999
于 2020-10-17T09:25:28.240 回答