Ask Your Question
1

Finding intersection of two lists?

asked 8 years ago

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.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 8 years ago

FrédéricC gravatar image

Like that:

[x for x in list0 if x in list1]
Preview: (hide)
link

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: 8 years ago

Seen: 2,349 times

Last updated: Mar 04 '17