Ask Your Question
0

Finding intersection of two lists?

asked 2017-03-04 14:53:38 -0600

sssageeee gravatar image

Hello, guys. First of all, I'm very new to Sage and also to this forum, so please be generous for my question...

So, suppose we have

c=[[0, 1, 2, 3], [0, 1, 2, 4], [0, 1, 3, 4], [0, 2, 3, 4], [1, 2, 3, 4]],

d=[[0, 1, 2, 3], [0, 1, 3, 4], [0, 2, 3, 4], [9, 8, 7, 6]]

I would like to get

[[0, 1, 2, 3], [0, 1, 3, 4], [0, 2, 3, 4]]

which is intersection of c and d. Is there a way to do this?

Thank you for any help.

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2017-03-05 02:07:56 -0600

FrédéricC gravatar image

Like that:

[x for x in list0 if x in list1]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-03-04 14:48:54 -0600

Seen: 143 times

Last updated: Mar 04