Common elements between two lists with no duplicat

2020-05-01 03:36发布

Problem is this, take two lists, say for example these two:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

And write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes.

Here's my code:

a = [1, 1, 2, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
c = []
for i in a:
    if i in b and i not in c:
        c.append([i])
print(c)

My output is still giving me duplicates despite the 'i not in c' statement. why is this? I'm sure its blatantly obvious, I just cant see it!

3条回答
我命由我不由天
2楼-- · 2020-05-01 04:11

The below code would work:

newlist = []
for x in b:
    if x in a:
        if x in newlist:
            print("duplicate")
        else:
            newlist.append(x)

for y in newlist:
    print(y)   
查看更多
够拽才男人
3楼-- · 2020-05-01 04:22
  1. You are appending a list containing i to c, so i not in c will always return True. You should append i on its own: c.append(i)

Or

  1. Simply use sets (if order is not important):

    a = [1, 1, 2, 2, 3, 5, 8, 13, 21, 34, 55, 89]
    b = [1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
    c = set(a) & set(b)  #  & calculates the intersection.
    print(c)
    #  {1, 2, 3, 5, 8, 13}
    

EDIT As @Ev. Kounis suggested in the comment, you will gain some speed by using
c = set(a).intersection(b).

查看更多
Juvenile、少年°
4楼-- · 2020-05-01 04:32

Using intuition of sets, You could do something like this...

filtered_arr = list(set(b)-set(a))

First you convert 2 arrays into sets, then take the substitute of it convert the result into the list again.

查看更多
登录 后发表回答