Opposite of set.intersection in python?Solving 'is one away' with set()Get complement of three listsHow do I check whether a file exists without exceptions?Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to know if an object has an attribute in PythonDoes Python have a string 'contains' substring method?Delete an element from a dictionaryMost elegant way to check if the string is empty in Python?
A Note on N!
Phrase for the opposite of "foolproof"
Do I have an "anti-research" personality?
What does ゆーか mean?
How did Captain America manage to do this?
How to stop co-workers from teasing me because I know Russian?
Checks user level and limit the data before saving it to mongoDB
Mistake in years of experience in resume?
How to have a sharp product image?
What term is being referred to with "reflected-sound-of-underground-spirits"?
Was there a shared-world project before "Thieves World"?
What is the philosophical significance of speech acts/implicature?
"The cow" OR "a cow" OR "cows" in this context
Relationship between strut and baselineskip
Check if a string is entirely made of the same substring
What happened to Captain America in Endgame?
Is it idiomatic to construct against `this`
bldc motor, esc and battery draw, nominal vs peak
Multiple options vs single option UI
What does the integral of a function times a function of a random variable represent, conceptually?
Elements other than carbon that can form many different compounds by bonding to themselves?
Rivers without rain
How do I check if a string is entirely made of the same substring?
Pulling the rope with one hand is as heavy as with two hands?
Opposite of set.intersection in python?
Solving 'is one away' with set()Get complement of three listsHow do I check whether a file exists without exceptions?Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to know if an object has an attribute in PythonDoes Python have a string 'contains' substring method?Delete an element from a dictionaryMost elegant way to check if the string is empty in Python?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
In Python you can use a.intersection(b)
to find the items common to both sets.
Is there a way to do the disjoint opposite version of this? Items that are not common to both a
and b
; the unique items in a
unioned with the unique items in b
?
python set
add a comment |
In Python you can use a.intersection(b)
to find the items common to both sets.
Is there a way to do the disjoint opposite version of this? Items that are not common to both a
and b
; the unique items in a
unioned with the unique items in b
?
python set
add a comment |
In Python you can use a.intersection(b)
to find the items common to both sets.
Is there a way to do the disjoint opposite version of this? Items that are not common to both a
and b
; the unique items in a
unioned with the unique items in b
?
python set
In Python you can use a.intersection(b)
to find the items common to both sets.
Is there a way to do the disjoint opposite version of this? Items that are not common to both a
and b
; the unique items in a
unioned with the unique items in b
?
python set
python set
edited Apr 29 '15 at 15:18
Martijn Pieters♦
730k14625622361
730k14625622361
asked Apr 29 '15 at 15:13
user4847061user4847061
275137
275137
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
You are looking for the symmetric difference; all elements that appear only in set a or in set b, but not both:
a.symmetric_difference(b)
From the set.symmetric_difference()
method documentation:
Return a new set with elements in either the set or other but not both.
You can use the ^
operator too, if both a
and b
are sets:
a ^ b
while set.symmetric_difference()
takes any iterable for the other argument.
The output is the equivalent of (a | b) - (a & b)
, the union of both sets minus the intersection of both sets.
Isn't ^ normaly XOR operator?
– user4847061
Apr 29 '15 at 15:31
1
@user4847061: it is, but sets have overloaded several such operators.|
and&
are normally bitwise OR and bitwise AND, but on sets they give you the union and the intersection. The comparison operators<
,<=
,>
and>=
have been overloaded too.
– Martijn Pieters♦
Apr 29 '15 at 15:34
add a comment |
a=1,2,4,5,6
b=5,6,4,9
c=(a^b)&b
print(c) # you got 9
add a comment |
The best way is a list comprehension.
a = [ 1,2,3,4]
b = [ 8,7,9,2,1]
c = [ element for element in a if element not in b]
d = [ element for element in b if element not in a]
print(c)
# output is [ 3,4]
print(d)
# output is [8,7,9]
You can join both lists
add a comment |
Try this code for (set(a) - intersection(a&b))
a = [1,2,3,4,5,6]
b = [2,3]
for i in b:
if i in a:
a.remove(i)
print(a)
the output is [1,4,5,6]
I hope, it will work
It's usually bad to mutate lists you are iterating over (in this case, there is no real consequence, unless I only care about returning a new list and not modifyinga
). Alsocheck = i in a
is redundant since you can alwaysif i in a:
– cowbert
Mar 14 '18 at 20:26
@cowbert thanks for your advise. I have fixed it. I will learn more about that.
– Muhammad Ammar Fauzan
Mar 16 '18 at 23:11
add a comment |
e, f are two list you want to check disjoint
a = [1,2,3,4]
b = [8,7,9,2,1]
c = []
def loop_to_check(e,f):
for i in range(len(e)):
if e[i] not in f:
c.append(e[i])
loop_to_check(a,b)
loop_to_check(b,a)
print(c)
## output is [3,4,8,7,9]
This loops around to list and returns the disjoint list
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29947844%2fopposite-of-set-intersection-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are looking for the symmetric difference; all elements that appear only in set a or in set b, but not both:
a.symmetric_difference(b)
From the set.symmetric_difference()
method documentation:
Return a new set with elements in either the set or other but not both.
You can use the ^
operator too, if both a
and b
are sets:
a ^ b
while set.symmetric_difference()
takes any iterable for the other argument.
The output is the equivalent of (a | b) - (a & b)
, the union of both sets minus the intersection of both sets.
Isn't ^ normaly XOR operator?
– user4847061
Apr 29 '15 at 15:31
1
@user4847061: it is, but sets have overloaded several such operators.|
and&
are normally bitwise OR and bitwise AND, but on sets they give you the union and the intersection. The comparison operators<
,<=
,>
and>=
have been overloaded too.
– Martijn Pieters♦
Apr 29 '15 at 15:34
add a comment |
You are looking for the symmetric difference; all elements that appear only in set a or in set b, but not both:
a.symmetric_difference(b)
From the set.symmetric_difference()
method documentation:
Return a new set with elements in either the set or other but not both.
You can use the ^
operator too, if both a
and b
are sets:
a ^ b
while set.symmetric_difference()
takes any iterable for the other argument.
The output is the equivalent of (a | b) - (a & b)
, the union of both sets minus the intersection of both sets.
Isn't ^ normaly XOR operator?
– user4847061
Apr 29 '15 at 15:31
1
@user4847061: it is, but sets have overloaded several such operators.|
and&
are normally bitwise OR and bitwise AND, but on sets they give you the union and the intersection. The comparison operators<
,<=
,>
and>=
have been overloaded too.
– Martijn Pieters♦
Apr 29 '15 at 15:34
add a comment |
You are looking for the symmetric difference; all elements that appear only in set a or in set b, but not both:
a.symmetric_difference(b)
From the set.symmetric_difference()
method documentation:
Return a new set with elements in either the set or other but not both.
You can use the ^
operator too, if both a
and b
are sets:
a ^ b
while set.symmetric_difference()
takes any iterable for the other argument.
The output is the equivalent of (a | b) - (a & b)
, the union of both sets minus the intersection of both sets.
You are looking for the symmetric difference; all elements that appear only in set a or in set b, but not both:
a.symmetric_difference(b)
From the set.symmetric_difference()
method documentation:
Return a new set with elements in either the set or other but not both.
You can use the ^
operator too, if both a
and b
are sets:
a ^ b
while set.symmetric_difference()
takes any iterable for the other argument.
The output is the equivalent of (a | b) - (a & b)
, the union of both sets minus the intersection of both sets.
edited May 9 '17 at 14:27
answered Apr 29 '15 at 15:15
Martijn Pieters♦Martijn Pieters
730k14625622361
730k14625622361
Isn't ^ normaly XOR operator?
– user4847061
Apr 29 '15 at 15:31
1
@user4847061: it is, but sets have overloaded several such operators.|
and&
are normally bitwise OR and bitwise AND, but on sets they give you the union and the intersection. The comparison operators<
,<=
,>
and>=
have been overloaded too.
– Martijn Pieters♦
Apr 29 '15 at 15:34
add a comment |
Isn't ^ normaly XOR operator?
– user4847061
Apr 29 '15 at 15:31
1
@user4847061: it is, but sets have overloaded several such operators.|
and&
are normally bitwise OR and bitwise AND, but on sets they give you the union and the intersection. The comparison operators<
,<=
,>
and>=
have been overloaded too.
– Martijn Pieters♦
Apr 29 '15 at 15:34
Isn't ^ normaly XOR operator?
– user4847061
Apr 29 '15 at 15:31
Isn't ^ normaly XOR operator?
– user4847061
Apr 29 '15 at 15:31
1
1
@user4847061: it is, but sets have overloaded several such operators.
|
and &
are normally bitwise OR and bitwise AND, but on sets they give you the union and the intersection. The comparison operators <
, <=
, >
and >=
have been overloaded too.– Martijn Pieters♦
Apr 29 '15 at 15:34
@user4847061: it is, but sets have overloaded several such operators.
|
and &
are normally bitwise OR and bitwise AND, but on sets they give you the union and the intersection. The comparison operators <
, <=
, >
and >=
have been overloaded too.– Martijn Pieters♦
Apr 29 '15 at 15:34
add a comment |
a=1,2,4,5,6
b=5,6,4,9
c=(a^b)&b
print(c) # you got 9
add a comment |
a=1,2,4,5,6
b=5,6,4,9
c=(a^b)&b
print(c) # you got 9
add a comment |
a=1,2,4,5,6
b=5,6,4,9
c=(a^b)&b
print(c) # you got 9
a=1,2,4,5,6
b=5,6,4,9
c=(a^b)&b
print(c) # you got 9
answered May 28 '18 at 4:39
Kaung Yar ZarKaung Yar Zar
111
111
add a comment |
add a comment |
The best way is a list comprehension.
a = [ 1,2,3,4]
b = [ 8,7,9,2,1]
c = [ element for element in a if element not in b]
d = [ element for element in b if element not in a]
print(c)
# output is [ 3,4]
print(d)
# output is [8,7,9]
You can join both lists
add a comment |
The best way is a list comprehension.
a = [ 1,2,3,4]
b = [ 8,7,9,2,1]
c = [ element for element in a if element not in b]
d = [ element for element in b if element not in a]
print(c)
# output is [ 3,4]
print(d)
# output is [8,7,9]
You can join both lists
add a comment |
The best way is a list comprehension.
a = [ 1,2,3,4]
b = [ 8,7,9,2,1]
c = [ element for element in a if element not in b]
d = [ element for element in b if element not in a]
print(c)
# output is [ 3,4]
print(d)
# output is [8,7,9]
You can join both lists
The best way is a list comprehension.
a = [ 1,2,3,4]
b = [ 8,7,9,2,1]
c = [ element for element in a if element not in b]
d = [ element for element in b if element not in a]
print(c)
# output is [ 3,4]
print(d)
# output is [8,7,9]
You can join both lists
answered Mar 22 at 17:24
ApeasantApeasant
213
213
add a comment |
add a comment |
Try this code for (set(a) - intersection(a&b))
a = [1,2,3,4,5,6]
b = [2,3]
for i in b:
if i in a:
a.remove(i)
print(a)
the output is [1,4,5,6]
I hope, it will work
It's usually bad to mutate lists you are iterating over (in this case, there is no real consequence, unless I only care about returning a new list and not modifyinga
). Alsocheck = i in a
is redundant since you can alwaysif i in a:
– cowbert
Mar 14 '18 at 20:26
@cowbert thanks for your advise. I have fixed it. I will learn more about that.
– Muhammad Ammar Fauzan
Mar 16 '18 at 23:11
add a comment |
Try this code for (set(a) - intersection(a&b))
a = [1,2,3,4,5,6]
b = [2,3]
for i in b:
if i in a:
a.remove(i)
print(a)
the output is [1,4,5,6]
I hope, it will work
It's usually bad to mutate lists you are iterating over (in this case, there is no real consequence, unless I only care about returning a new list and not modifyinga
). Alsocheck = i in a
is redundant since you can alwaysif i in a:
– cowbert
Mar 14 '18 at 20:26
@cowbert thanks for your advise. I have fixed it. I will learn more about that.
– Muhammad Ammar Fauzan
Mar 16 '18 at 23:11
add a comment |
Try this code for (set(a) - intersection(a&b))
a = [1,2,3,4,5,6]
b = [2,3]
for i in b:
if i in a:
a.remove(i)
print(a)
the output is [1,4,5,6]
I hope, it will work
Try this code for (set(a) - intersection(a&b))
a = [1,2,3,4,5,6]
b = [2,3]
for i in b:
if i in a:
a.remove(i)
print(a)
the output is [1,4,5,6]
I hope, it will work
edited Mar 16 '18 at 23:09
answered Mar 11 '18 at 11:00
Muhammad Ammar FauzanMuhammad Ammar Fauzan
614
614
It's usually bad to mutate lists you are iterating over (in this case, there is no real consequence, unless I only care about returning a new list and not modifyinga
). Alsocheck = i in a
is redundant since you can alwaysif i in a:
– cowbert
Mar 14 '18 at 20:26
@cowbert thanks for your advise. I have fixed it. I will learn more about that.
– Muhammad Ammar Fauzan
Mar 16 '18 at 23:11
add a comment |
It's usually bad to mutate lists you are iterating over (in this case, there is no real consequence, unless I only care about returning a new list and not modifyinga
). Alsocheck = i in a
is redundant since you can alwaysif i in a:
– cowbert
Mar 14 '18 at 20:26
@cowbert thanks for your advise. I have fixed it. I will learn more about that.
– Muhammad Ammar Fauzan
Mar 16 '18 at 23:11
It's usually bad to mutate lists you are iterating over (in this case, there is no real consequence, unless I only care about returning a new list and not modifying
a
). Also check = i in a
is redundant since you can always if i in a:
– cowbert
Mar 14 '18 at 20:26
It's usually bad to mutate lists you are iterating over (in this case, there is no real consequence, unless I only care about returning a new list and not modifying
a
). Also check = i in a
is redundant since you can always if i in a:
– cowbert
Mar 14 '18 at 20:26
@cowbert thanks for your advise. I have fixed it. I will learn more about that.
– Muhammad Ammar Fauzan
Mar 16 '18 at 23:11
@cowbert thanks for your advise. I have fixed it. I will learn more about that.
– Muhammad Ammar Fauzan
Mar 16 '18 at 23:11
add a comment |
e, f are two list you want to check disjoint
a = [1,2,3,4]
b = [8,7,9,2,1]
c = []
def loop_to_check(e,f):
for i in range(len(e)):
if e[i] not in f:
c.append(e[i])
loop_to_check(a,b)
loop_to_check(b,a)
print(c)
## output is [3,4,8,7,9]
This loops around to list and returns the disjoint list
add a comment |
e, f are two list you want to check disjoint
a = [1,2,3,4]
b = [8,7,9,2,1]
c = []
def loop_to_check(e,f):
for i in range(len(e)):
if e[i] not in f:
c.append(e[i])
loop_to_check(a,b)
loop_to_check(b,a)
print(c)
## output is [3,4,8,7,9]
This loops around to list and returns the disjoint list
add a comment |
e, f are two list you want to check disjoint
a = [1,2,3,4]
b = [8,7,9,2,1]
c = []
def loop_to_check(e,f):
for i in range(len(e)):
if e[i] not in f:
c.append(e[i])
loop_to_check(a,b)
loop_to_check(b,a)
print(c)
## output is [3,4,8,7,9]
This loops around to list and returns the disjoint list
e, f are two list you want to check disjoint
a = [1,2,3,4]
b = [8,7,9,2,1]
c = []
def loop_to_check(e,f):
for i in range(len(e)):
if e[i] not in f:
c.append(e[i])
loop_to_check(a,b)
loop_to_check(b,a)
print(c)
## output is [3,4,8,7,9]
This loops around to list and returns the disjoint list
answered Aug 31 '18 at 5:16
chandra singhchandra singh
133
133
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29947844%2fopposite-of-set-intersection-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown