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?










share|improve this question




























    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?










    share|improve this question


























      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?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 at 0:31

























      asked Nov 13 at 0:18









      Ruturaj

      356




      356
























          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}





          share|improve this answer























          • 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 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




            @blhsing This makes more sense to me, thanks for your explanation.
            – Ruturaj
            Nov 13 at 0:44




















          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)





          share|improve this answer





















          • 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




            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











          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',
          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
          });


          }
          });














           

          draft saved


          draft discarded


















          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








          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}





          share|improve this answer























          • 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 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




            @blhsing This makes more sense to me, thanks for your explanation.
            – Ruturaj
            Nov 13 at 0:44

















          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}





          share|improve this answer























          • 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 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




            @blhsing This makes more sense to me, thanks for your explanation.
            – Ruturaj
            Nov 13 at 0:44















          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}





          share|improve this answer














          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}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 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




            @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






          • 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








          • 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














          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)





          share|improve this answer





















          • 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




            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















          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)





          share|improve this answer





















          • 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




            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













          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)





          share|improve this answer












          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)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 at 0:25









          iElden

          42814




          42814












          • 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




            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








          • 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


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

          ComboBox Display Member on multiple fields

          Is it possible to collect Nectar points via Trainline?