Ask Your Question
1

Finding intersection of two lists?

asked 2017-03-04 21:53:38 +0100

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 09:07:56 +0100

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 21:48:54 +0100

Seen: 2,235 times

Last updated: Mar 04 '17