complementary lists?

R

Ross

If I have a list x = [1,2,3,4,5,6,7,8,9] and another list that is a
subset of x: y = [1,4,7] , is there a quick way that I could return
the complementary subset to y.... z=[2,3,5,6,8,9] ?

The reason I ask is because I have a generator function that generates
a list of tuples and I would like to divide this list into
complementary lists.
 
K

Kay Schluehr

If I have a list x = [1,2,3,4,5,6,7,8,9] and another list that is a
subset of x: y = [1,4,7] , is there a quick way that I could return
the complementary subset to y.... z=[2,3,5,6,8,9] ?

The reason I ask is because I have a generator function that generates
a list of tuples and I would like to divide this list into
complementary lists.

z = [u for u in x if u not in y]

or

z = [u for u in x if u not in set(y)]

Since you are dealing with tuples and a natural order might not be
that relevant you can also set objects in the first place:

z = x - y
 
M

Mensanator

If I have a list x = [1,2,3,4,5,6,7,8,9] and another list that is a
subset of x: �y = [1,4,7] , is there a quick way that I could return
the complementary subset to y.... z=[2,3,5,6,8,9] ?

The reason I ask is because I have a generator function that generates
a list of tuples and I would like to divide this list into
complementary lists.
s = set([1,2,3,4,5,6,7,8,9])
s.difference(set([1,4,7]))

set([2, 3, 5, 6, 8, 9])
 
P

Paul Rubin

Ross said:
If I have a list x = [1,2,3,4,5,6,7,8,9] and another list that is a
subset of x: y = [1,4,7] , is there a quick way that I could return
the complementary subset to y.... z=[2,3,5,6,8,9] ?
>>> x = [1,2,3,4,5,6,7,8,9]
>>> y = [1,4,7]
>>> print sorted(set(x)-set(y))
[2, 3, 5, 6, 8, 9]
 
A

Arnaud Delobelle

Kay Schluehr said:
If I have a list x = [1,2,3,4,5,6,7,8,9] and another list that is a
subset of x: y = [1,4,7] , is there a quick way that I could return
the complementary subset to y.... z=[2,3,5,6,8,9] ?

The reason I ask is because I have a generator function that generates
a list of tuples and I would like to divide this list into
complementary lists.

z = [u for u in x if u not in y]

or

z = [u for u in x if u not in set(y)]

The above will evaluate set(y) for each element in x.

s = set(y)
z = [u for u in x if u not in s]
 
B

Bryan

Kay Schluehr said:
If I have a list x = [1,2,3,4,5,6,7,8,9] and another list that is a
subset of x:  y = [1,4,7] , is there a quick way that I could return
the complementary subset to y.... z=[2,3,5,6,8,9] ?
The reason I ask is because I have a generator function that generates
a list of tuples and I would like to divide this list into
complementary lists.
z = [u for u in x if u not in y]

z = [u for u in x if u not in set(y)]

The above will evaluate set(y) for each element in x.

s = set(y)
z = [u for u in x if u not in s]

--
Arnaud
y = [1, 2, 3, 4, 5]
s = set(y)
s.complimentary()
['you are handsome', 'you are smart', 'nice jacket']
 
B

Bryan

Kay Schluehr said:
If I have a list x = [1,2,3,4,5,6,7,8,9] and another list that is a
subset of x:  y = [1,4,7] , is there a quick way that I could return
the complementary subset to y.... z=[2,3,5,6,8,9] ?
The reason I ask is because I have a generator function that generates
a list of tuples and I would like to divide this list into
complementary lists.
z = [u for u in x if u not in y]

z = [u for u in x if u not in set(y)]

The above will evaluate set(y) for each element in x.

s = set(y)
z = [u for u in x if u not in s]

--
Arnaud
ls = [1, 2, 3, 4, 5]
s = set(ls)
s.complimentary()
['your are handsome', 'your are smart', 'nice jacket'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top