conflicting cases in python lists
up vote
4
down vote
favorite
I have two lists of sets -
attribute = [{0, 1, 2, 3, 6, 7}, {4, 5}]
and
decision = [{0, 1, 2}, {3, 4}, {5}, {6, 7}]
I want -
{3, 4}
Here, {3, 4}
is conflicting, as it is neither a subset of {0, 1, 2, 3, 6, 7}
, nor of {4, 5}
.
My code -
check =
for i in attribute:
for j in decision:
if j.issubset(i):
check.append(j)
print(check)
for x in decision:
if not x in check:
temp = x
print(temp)
This gives me {3, 4}
, but is there any easier (and/ or) faster way to do this?
python-3.x nested-lists
add a comment |
up vote
4
down vote
favorite
I have two lists of sets -
attribute = [{0, 1, 2, 3, 6, 7}, {4, 5}]
and
decision = [{0, 1, 2}, {3, 4}, {5}, {6, 7}]
I want -
{3, 4}
Here, {3, 4}
is conflicting, as it is neither a subset of {0, 1, 2, 3, 6, 7}
, nor of {4, 5}
.
My code -
check =
for i in attribute:
for j in decision:
if j.issubset(i):
check.append(j)
print(check)
for x in decision:
if not x in check:
temp = x
print(temp)
This gives me {3, 4}
, but is there any easier (and/ or) faster way to do this?
python-3.x nested-lists
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I have two lists of sets -
attribute = [{0, 1, 2, 3, 6, 7}, {4, 5}]
and
decision = [{0, 1, 2}, {3, 4}, {5}, {6, 7}]
I want -
{3, 4}
Here, {3, 4}
is conflicting, as it is neither a subset of {0, 1, 2, 3, 6, 7}
, nor of {4, 5}
.
My code -
check =
for i in attribute:
for j in decision:
if j.issubset(i):
check.append(j)
print(check)
for x in decision:
if not x in check:
temp = x
print(temp)
This gives me {3, 4}
, but is there any easier (and/ or) faster way to do this?
python-3.x nested-lists
I have two lists of sets -
attribute = [{0, 1, 2, 3, 6, 7}, {4, 5}]
and
decision = [{0, 1, 2}, {3, 4}, {5}, {6, 7}]
I want -
{3, 4}
Here, {3, 4}
is conflicting, as it is neither a subset of {0, 1, 2, 3, 6, 7}
, nor of {4, 5}
.
My code -
check =
for i in attribute:
for j in decision:
if j.issubset(i):
check.append(j)
print(check)
for x in decision:
if not x in check:
temp = x
print(temp)
This gives me {3, 4}
, but is there any easier (and/ or) faster way to do this?
python-3.x nested-lists
python-3.x nested-lists
edited Nov 13 at 0:31
asked Nov 13 at 0:18
Ruturaj
356
356
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
You can use the following list comprehension:
[d for d in decision if not any(d <= a for a in attribute)]
This returns:
[{3, 4}]
If you want just the first set that satisfies the criteria, you can use next
with a generator expression instead:
next(d for d in decision if not any(d <= a for a in attribute))
This returns:
{3, 4}
thanks @blhsing. But I assume it will take some more time to convert it from[{3, 4}]
to{3, 4}
– Ruturaj
Nov 13 at 0:36
1
But there could be more than one set in thedecision
list that is not a subset of any of of the sets in theattribute
list. Returning just{3, 4}
makes little sense unless you are guaranteed that there is always exactly one such set that satisfies your criteria. That said, I've updated my answer with a solution that would return what you want, as long as you are sure that you only want the first matching set, or there is always only one matching set.
– blhsing
Nov 13 at 0:42
1
@blhsing This makes more sense to me, thanks for your explanation.
– Ruturaj
Nov 13 at 0:44
add a comment |
up vote
1
down vote
result = [i for i in decision if not [j for j in attribute if i.issubset(j)]]
result is the list of all set they are not a subset of attribute. :)
This is the compact version of :
result =
for i in decision:
tmp_list =
for j in attribute:
if i.issubset(j):
tmp_list.append(j)
if not tmp_list:
result.append(i)
Thanks, I triedresult = [i for i in decision if not [j for j in attribute if i.issubset(j)]]
which gives me[{3, 4}]
, but not{3,4}
(I know the conversion is possible). But is it faster than my solution (with additional conversion time)?
– Ruturaj
Nov 13 at 0:34
1
EDIT : sorry, my last calcul was wrong, our program has same execution time after convertion on my computer : 0.030 - 0.050 sec
– iElden
Nov 13 at 0:38
Thanks @ iElden i upvoted, but @blhsing's explanation makes more sense to me.
– Ruturaj
Nov 13 at 0:47
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can use the following list comprehension:
[d for d in decision if not any(d <= a for a in attribute)]
This returns:
[{3, 4}]
If you want just the first set that satisfies the criteria, you can use next
with a generator expression instead:
next(d for d in decision if not any(d <= a for a in attribute))
This returns:
{3, 4}
thanks @blhsing. But I assume it will take some more time to convert it from[{3, 4}]
to{3, 4}
– Ruturaj
Nov 13 at 0:36
1
But there could be more than one set in thedecision
list that is not a subset of any of of the sets in theattribute
list. Returning just{3, 4}
makes little sense unless you are guaranteed that there is always exactly one such set that satisfies your criteria. That said, I've updated my answer with a solution that would return what you want, as long as you are sure that you only want the first matching set, or there is always only one matching set.
– blhsing
Nov 13 at 0:42
1
@blhsing This makes more sense to me, thanks for your explanation.
– Ruturaj
Nov 13 at 0:44
add a comment |
up vote
2
down vote
accepted
You can use the following list comprehension:
[d for d in decision if not any(d <= a for a in attribute)]
This returns:
[{3, 4}]
If you want just the first set that satisfies the criteria, you can use next
with a generator expression instead:
next(d for d in decision if not any(d <= a for a in attribute))
This returns:
{3, 4}
thanks @blhsing. But I assume it will take some more time to convert it from[{3, 4}]
to{3, 4}
– Ruturaj
Nov 13 at 0:36
1
But there could be more than one set in thedecision
list that is not a subset of any of of the sets in theattribute
list. Returning just{3, 4}
makes little sense unless you are guaranteed that there is always exactly one such set that satisfies your criteria. That said, I've updated my answer with a solution that would return what you want, as long as you are sure that you only want the first matching set, or there is always only one matching set.
– blhsing
Nov 13 at 0:42
1
@blhsing This makes more sense to me, thanks for your explanation.
– Ruturaj
Nov 13 at 0:44
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can use the following list comprehension:
[d for d in decision if not any(d <= a for a in attribute)]
This returns:
[{3, 4}]
If you want just the first set that satisfies the criteria, you can use next
with a generator expression instead:
next(d for d in decision if not any(d <= a for a in attribute))
This returns:
{3, 4}
You can use the following list comprehension:
[d for d in decision if not any(d <= a for a in attribute)]
This returns:
[{3, 4}]
If you want just the first set that satisfies the criteria, you can use next
with a generator expression instead:
next(d for d in decision if not any(d <= a for a in attribute))
This returns:
{3, 4}
edited Nov 13 at 0:45
answered Nov 13 at 0:34
blhsing
26.9k41335
26.9k41335
thanks @blhsing. But I assume it will take some more time to convert it from[{3, 4}]
to{3, 4}
– Ruturaj
Nov 13 at 0:36
1
But there could be more than one set in thedecision
list that is not a subset of any of of the sets in theattribute
list. Returning just{3, 4}
makes little sense unless you are guaranteed that there is always exactly one such set that satisfies your criteria. That said, I've updated my answer with a solution that would return what you want, as long as you are sure that you only want the first matching set, or there is always only one matching set.
– blhsing
Nov 13 at 0:42
1
@blhsing This makes more sense to me, thanks for your explanation.
– Ruturaj
Nov 13 at 0:44
add a comment |
thanks @blhsing. But I assume it will take some more time to convert it from[{3, 4}]
to{3, 4}
– Ruturaj
Nov 13 at 0:36
1
But there could be more than one set in thedecision
list that is not a subset of any of of the sets in theattribute
list. Returning just{3, 4}
makes little sense unless you are guaranteed that there is always exactly one such set that satisfies your criteria. That said, I've updated my answer with a solution that would return what you want, as long as you are sure that you only want the first matching set, or there is always only one matching set.
– blhsing
Nov 13 at 0:42
1
@blhsing This makes more sense to me, thanks for your explanation.
– Ruturaj
Nov 13 at 0:44
thanks @blhsing. But I assume it will take some more time to convert it from
[{3, 4}]
to {3, 4}
– Ruturaj
Nov 13 at 0:36
thanks @blhsing. But I assume it will take some more time to convert it from
[{3, 4}]
to {3, 4}
– Ruturaj
Nov 13 at 0:36
1
1
But there could be more than one set in the
decision
list that is not a subset of any of of the sets in the attribute
list. Returning just {3, 4}
makes little sense unless you are guaranteed that there is always exactly one such set that satisfies your criteria. That said, I've updated my answer with a solution that would return what you want, as long as you are sure that you only want the first matching set, or there is always only one matching set.– blhsing
Nov 13 at 0:42
But there could be more than one set in the
decision
list that is not a subset of any of of the sets in the attribute
list. Returning just {3, 4}
makes little sense unless you are guaranteed that there is always exactly one such set that satisfies your criteria. That said, I've updated my answer with a solution that would return what you want, as long as you are sure that you only want the first matching set, or there is always only one matching set.– blhsing
Nov 13 at 0:42
1
1
@blhsing This makes more sense to me, thanks for your explanation.
– Ruturaj
Nov 13 at 0:44
@blhsing This makes more sense to me, thanks for your explanation.
– Ruturaj
Nov 13 at 0:44
add a comment |
up vote
1
down vote
result = [i for i in decision if not [j for j in attribute if i.issubset(j)]]
result is the list of all set they are not a subset of attribute. :)
This is the compact version of :
result =
for i in decision:
tmp_list =
for j in attribute:
if i.issubset(j):
tmp_list.append(j)
if not tmp_list:
result.append(i)
Thanks, I triedresult = [i for i in decision if not [j for j in attribute if i.issubset(j)]]
which gives me[{3, 4}]
, but not{3,4}
(I know the conversion is possible). But is it faster than my solution (with additional conversion time)?
– Ruturaj
Nov 13 at 0:34
1
EDIT : sorry, my last calcul was wrong, our program has same execution time after convertion on my computer : 0.030 - 0.050 sec
– iElden
Nov 13 at 0:38
Thanks @ iElden i upvoted, but @blhsing's explanation makes more sense to me.
– Ruturaj
Nov 13 at 0:47
add a comment |
up vote
1
down vote
result = [i for i in decision if not [j for j in attribute if i.issubset(j)]]
result is the list of all set they are not a subset of attribute. :)
This is the compact version of :
result =
for i in decision:
tmp_list =
for j in attribute:
if i.issubset(j):
tmp_list.append(j)
if not tmp_list:
result.append(i)
Thanks, I triedresult = [i for i in decision if not [j for j in attribute if i.issubset(j)]]
which gives me[{3, 4}]
, but not{3,4}
(I know the conversion is possible). But is it faster than my solution (with additional conversion time)?
– Ruturaj
Nov 13 at 0:34
1
EDIT : sorry, my last calcul was wrong, our program has same execution time after convertion on my computer : 0.030 - 0.050 sec
– iElden
Nov 13 at 0:38
Thanks @ iElden i upvoted, but @blhsing's explanation makes more sense to me.
– Ruturaj
Nov 13 at 0:47
add a comment |
up vote
1
down vote
up vote
1
down vote
result = [i for i in decision if not [j for j in attribute if i.issubset(j)]]
result is the list of all set they are not a subset of attribute. :)
This is the compact version of :
result =
for i in decision:
tmp_list =
for j in attribute:
if i.issubset(j):
tmp_list.append(j)
if not tmp_list:
result.append(i)
result = [i for i in decision if not [j for j in attribute if i.issubset(j)]]
result is the list of all set they are not a subset of attribute. :)
This is the compact version of :
result =
for i in decision:
tmp_list =
for j in attribute:
if i.issubset(j):
tmp_list.append(j)
if not tmp_list:
result.append(i)
answered Nov 13 at 0:25
iElden
42814
42814
Thanks, I triedresult = [i for i in decision if not [j for j in attribute if i.issubset(j)]]
which gives me[{3, 4}]
, but not{3,4}
(I know the conversion is possible). But is it faster than my solution (with additional conversion time)?
– Ruturaj
Nov 13 at 0:34
1
EDIT : sorry, my last calcul was wrong, our program has same execution time after convertion on my computer : 0.030 - 0.050 sec
– iElden
Nov 13 at 0:38
Thanks @ iElden i upvoted, but @blhsing's explanation makes more sense to me.
– Ruturaj
Nov 13 at 0:47
add a comment |
Thanks, I triedresult = [i for i in decision if not [j for j in attribute if i.issubset(j)]]
which gives me[{3, 4}]
, but not{3,4}
(I know the conversion is possible). But is it faster than my solution (with additional conversion time)?
– Ruturaj
Nov 13 at 0:34
1
EDIT : sorry, my last calcul was wrong, our program has same execution time after convertion on my computer : 0.030 - 0.050 sec
– iElden
Nov 13 at 0:38
Thanks @ iElden i upvoted, but @blhsing's explanation makes more sense to me.
– Ruturaj
Nov 13 at 0:47
Thanks, I tried
result = [i for i in decision if not [j for j in attribute if i.issubset(j)]]
which gives me [{3, 4}]
, but not {3,4}
(I know the conversion is possible). But is it faster than my solution (with additional conversion time)?– Ruturaj
Nov 13 at 0:34
Thanks, I tried
result = [i for i in decision if not [j for j in attribute if i.issubset(j)]]
which gives me [{3, 4}]
, but not {3,4}
(I know the conversion is possible). But is it faster than my solution (with additional conversion time)?– Ruturaj
Nov 13 at 0:34
1
1
EDIT : sorry, my last calcul was wrong, our program has same execution time after convertion on my computer : 0.030 - 0.050 sec
– iElden
Nov 13 at 0:38
EDIT : sorry, my last calcul was wrong, our program has same execution time after convertion on my computer : 0.030 - 0.050 sec
– iElden
Nov 13 at 0:38
Thanks @ iElden i upvoted, but @blhsing's explanation makes more sense to me.
– Ruturaj
Nov 13 at 0:47
Thanks @ iElden i upvoted, but @blhsing's explanation makes more sense to me.
– Ruturaj
Nov 13 at 0:47
add a comment |
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%2f53272008%2fconflicting-cases-in-python-lists%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