conflicting cases in python 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
add a comment |
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 |
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 '18 at 0:31
Ruturaj
asked Nov 13 '18 at 0:18
RuturajRuturaj
66110
66110
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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]
to3, 4
– Ruturaj
Nov 13 '18 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 just3, 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 '18 at 0:42
1
@blhsing This makes more sense to me, thanks for your explanation.
– Ruturaj
Nov 13 '18 at 0:44
add a comment |
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 not3,4
(I know the conversion is possible). But is it faster than my solution (with additional conversion time)?
– Ruturaj
Nov 13 '18 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 '18 at 0:38
Thanks @ iElden i upvoted, but @blhsing's explanation makes more sense to me.
– Ruturaj
Nov 13 '18 at 0:47
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%2f53272008%2fconflicting-cases-in-python-lists%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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]
to3, 4
– Ruturaj
Nov 13 '18 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 just3, 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 '18 at 0:42
1
@blhsing This makes more sense to me, thanks for your explanation.
– Ruturaj
Nov 13 '18 at 0:44
add a comment |
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]
to3, 4
– Ruturaj
Nov 13 '18 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 just3, 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 '18 at 0:42
1
@blhsing This makes more sense to me, thanks for your explanation.
– Ruturaj
Nov 13 '18 at 0:44
add a comment |
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 '18 at 0:45
answered Nov 13 '18 at 0:34
blhsingblhsing
30k41336
30k41336
thanks @blhsing. But I assume it will take some more time to convert it from[3, 4]
to3, 4
– Ruturaj
Nov 13 '18 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 just3, 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 '18 at 0:42
1
@blhsing This makes more sense to me, thanks for your explanation.
– Ruturaj
Nov 13 '18 at 0:44
add a comment |
thanks @blhsing. But I assume it will take some more time to convert it from[3, 4]
to3, 4
– Ruturaj
Nov 13 '18 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 just3, 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 '18 at 0:42
1
@blhsing This makes more sense to me, thanks for your explanation.
– Ruturaj
Nov 13 '18 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 '18 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 '18 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 '18 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 '18 at 0:42
1
1
@blhsing This makes more sense to me, thanks for your explanation.
– Ruturaj
Nov 13 '18 at 0:44
@blhsing This makes more sense to me, thanks for your explanation.
– Ruturaj
Nov 13 '18 at 0:44
add a comment |
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 not3,4
(I know the conversion is possible). But is it faster than my solution (with additional conversion time)?
– Ruturaj
Nov 13 '18 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 '18 at 0:38
Thanks @ iElden i upvoted, but @blhsing's explanation makes more sense to me.
– Ruturaj
Nov 13 '18 at 0:47
add a comment |
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 not3,4
(I know the conversion is possible). But is it faster than my solution (with additional conversion time)?
– Ruturaj
Nov 13 '18 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 '18 at 0:38
Thanks @ iElden i upvoted, but @blhsing's explanation makes more sense to me.
– Ruturaj
Nov 13 '18 at 0:47
add a comment |
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 '18 at 0:25
iEldeniElden
680417
680417
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 not3,4
(I know the conversion is possible). But is it faster than my solution (with additional conversion time)?
– Ruturaj
Nov 13 '18 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 '18 at 0:38
Thanks @ iElden i upvoted, but @blhsing's explanation makes more sense to me.
– Ruturaj
Nov 13 '18 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 not3,4
(I know the conversion is possible). But is it faster than my solution (with additional conversion time)?
– Ruturaj
Nov 13 '18 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 '18 at 0:38
Thanks @ iElden i upvoted, but @blhsing's explanation makes more sense to me.
– Ruturaj
Nov 13 '18 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 '18 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 '18 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 '18 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 '18 at 0:38
Thanks @ iElden i upvoted, but @blhsing's explanation makes more sense to me.
– Ruturaj
Nov 13 '18 at 0:47
Thanks @ iElden i upvoted, but @blhsing's explanation makes more sense to me.
– Ruturaj
Nov 13 '18 at 0:47
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%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