Compare Dictionary Values that belongs to different keys









up vote
0
down vote

favorite












I have a dictionaries inside a list like this:



sample_dict = [1: [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]],
2: [[3, 4, 6, 7, 8, 9, 10, 11], [1, 2, 3, 6, 10], ]]


Now, I would like to check the key 1's first value in the list with key 2's first value.
something like this,



Compare Values (first value of list of lists of key 1)



[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 


with (first value of list of lists of key 2)



[3, 4, 6, 7, 8, 9, 10, 11]


If they are a match I would like to append it to a new list matching_list, if not I would to append non-matching values into another list non_matching_list.



This is what I tried so far,



matching_list = 
non_matching_list =

for each_dict in sample_dict:
current_dict_values =
for key, value_list in each_dict.items():
temp_dict_values =

for value in value_list:
temp_dict_values.append(value)

.... don't know how to keep track of key 1's first list of lists values.


I was thinking of creating a temporary list to keep track of key 1 list values, but I am stuck and not sure how to proceed.



My final output should be like this:



matching_list = [[3,4,6,7,8,9,10], [1,2,3], ]
non_matching_list = [[1,2,5,11],[4,5,6,10],[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]


How can I achieve my output? Any ideas would be great.










share|improve this question



























    up vote
    0
    down vote

    favorite












    I have a dictionaries inside a list like this:



    sample_dict = [1: [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 
    [1, 2, 3, 4, 5],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]],
    2: [[3, 4, 6, 7, 8, 9, 10, 11], [1, 2, 3, 6, 10], ]]


    Now, I would like to check the key 1's first value in the list with key 2's first value.
    something like this,



    Compare Values (first value of list of lists of key 1)



    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 


    with (first value of list of lists of key 2)



    [3, 4, 6, 7, 8, 9, 10, 11]


    If they are a match I would like to append it to a new list matching_list, if not I would to append non-matching values into another list non_matching_list.



    This is what I tried so far,



    matching_list = 
    non_matching_list =

    for each_dict in sample_dict:
    current_dict_values =
    for key, value_list in each_dict.items():
    temp_dict_values =

    for value in value_list:
    temp_dict_values.append(value)

    .... don't know how to keep track of key 1's first list of lists values.


    I was thinking of creating a temporary list to keep track of key 1 list values, but I am stuck and not sure how to proceed.



    My final output should be like this:



    matching_list = [[3,4,6,7,8,9,10], [1,2,3], ]
    non_matching_list = [[1,2,5,11],[4,5,6,10],[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]


    How can I achieve my output? Any ideas would be great.










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a dictionaries inside a list like this:



      sample_dict = [1: [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 
      [1, 2, 3, 4, 5],
      [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]],
      2: [[3, 4, 6, 7, 8, 9, 10, 11], [1, 2, 3, 6, 10], ]]


      Now, I would like to check the key 1's first value in the list with key 2's first value.
      something like this,



      Compare Values (first value of list of lists of key 1)



      [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 


      with (first value of list of lists of key 2)



      [3, 4, 6, 7, 8, 9, 10, 11]


      If they are a match I would like to append it to a new list matching_list, if not I would to append non-matching values into another list non_matching_list.



      This is what I tried so far,



      matching_list = 
      non_matching_list =

      for each_dict in sample_dict:
      current_dict_values =
      for key, value_list in each_dict.items():
      temp_dict_values =

      for value in value_list:
      temp_dict_values.append(value)

      .... don't know how to keep track of key 1's first list of lists values.


      I was thinking of creating a temporary list to keep track of key 1 list values, but I am stuck and not sure how to proceed.



      My final output should be like this:



      matching_list = [[3,4,6,7,8,9,10], [1,2,3], ]
      non_matching_list = [[1,2,5,11],[4,5,6,10],[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]


      How can I achieve my output? Any ideas would be great.










      share|improve this question















      I have a dictionaries inside a list like this:



      sample_dict = [1: [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 
      [1, 2, 3, 4, 5],
      [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]],
      2: [[3, 4, 6, 7, 8, 9, 10, 11], [1, 2, 3, 6, 10], ]]


      Now, I would like to check the key 1's first value in the list with key 2's first value.
      something like this,



      Compare Values (first value of list of lists of key 1)



      [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 


      with (first value of list of lists of key 2)



      [3, 4, 6, 7, 8, 9, 10, 11]


      If they are a match I would like to append it to a new list matching_list, if not I would to append non-matching values into another list non_matching_list.



      This is what I tried so far,



      matching_list = 
      non_matching_list =

      for each_dict in sample_dict:
      current_dict_values =
      for key, value_list in each_dict.items():
      temp_dict_values =

      for value in value_list:
      temp_dict_values.append(value)

      .... don't know how to keep track of key 1's first list of lists values.


      I was thinking of creating a temporary list to keep track of key 1 list values, but I am stuck and not sure how to proceed.



      My final output should be like this:



      matching_list = [[3,4,6,7,8,9,10], [1,2,3], ]
      non_matching_list = [[1,2,5,11],[4,5,6,10],[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]


      How can I achieve my output? Any ideas would be great.







      python python-3.x list loops dictionary






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 21:04









      rockikz

      148110




      148110










      asked Nov 11 at 20:36









      user9431057

      1418




      1418






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          This can be achieved by converting lists to sets to make operations like symmetric_difference() and intersection() for your non_matching_list and matching_list respectively.



          Here is one of the solutions:



          matching_list, non_matching_list = , 

          for lists1, lists2 in zip(sample_dict[0].values(), sample_dict[1].values()):
          for l1, l2 in zip(lists1, lists2):
          matching_list.append(list(set(l1) & set(l2)))
          non_matching_list.append(list(set(l1).symmetric_difference(set(l2))))


          Note that using set(l1) & set(l2) is same as set(l1).intersection(set(l2)), so basically it's an intersection operation here.



          I'm also using builtin zip() function to aggregate elements from each of the iterables ( both lists ).






          share|improve this answer




















            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%2f53252994%2fcompare-dictionary-values-that-belongs-to-different-keys%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            This can be achieved by converting lists to sets to make operations like symmetric_difference() and intersection() for your non_matching_list and matching_list respectively.



            Here is one of the solutions:



            matching_list, non_matching_list = , 

            for lists1, lists2 in zip(sample_dict[0].values(), sample_dict[1].values()):
            for l1, l2 in zip(lists1, lists2):
            matching_list.append(list(set(l1) & set(l2)))
            non_matching_list.append(list(set(l1).symmetric_difference(set(l2))))


            Note that using set(l1) & set(l2) is same as set(l1).intersection(set(l2)), so basically it's an intersection operation here.



            I'm also using builtin zip() function to aggregate elements from each of the iterables ( both lists ).






            share|improve this answer
























              up vote
              1
              down vote



              accepted










              This can be achieved by converting lists to sets to make operations like symmetric_difference() and intersection() for your non_matching_list and matching_list respectively.



              Here is one of the solutions:



              matching_list, non_matching_list = , 

              for lists1, lists2 in zip(sample_dict[0].values(), sample_dict[1].values()):
              for l1, l2 in zip(lists1, lists2):
              matching_list.append(list(set(l1) & set(l2)))
              non_matching_list.append(list(set(l1).symmetric_difference(set(l2))))


              Note that using set(l1) & set(l2) is same as set(l1).intersection(set(l2)), so basically it's an intersection operation here.



              I'm also using builtin zip() function to aggregate elements from each of the iterables ( both lists ).






              share|improve this answer






















                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                This can be achieved by converting lists to sets to make operations like symmetric_difference() and intersection() for your non_matching_list and matching_list respectively.



                Here is one of the solutions:



                matching_list, non_matching_list = , 

                for lists1, lists2 in zip(sample_dict[0].values(), sample_dict[1].values()):
                for l1, l2 in zip(lists1, lists2):
                matching_list.append(list(set(l1) & set(l2)))
                non_matching_list.append(list(set(l1).symmetric_difference(set(l2))))


                Note that using set(l1) & set(l2) is same as set(l1).intersection(set(l2)), so basically it's an intersection operation here.



                I'm also using builtin zip() function to aggregate elements from each of the iterables ( both lists ).






                share|improve this answer












                This can be achieved by converting lists to sets to make operations like symmetric_difference() and intersection() for your non_matching_list and matching_list respectively.



                Here is one of the solutions:



                matching_list, non_matching_list = , 

                for lists1, lists2 in zip(sample_dict[0].values(), sample_dict[1].values()):
                for l1, l2 in zip(lists1, lists2):
                matching_list.append(list(set(l1) & set(l2)))
                non_matching_list.append(list(set(l1).symmetric_difference(set(l2))))


                Note that using set(l1) & set(l2) is same as set(l1).intersection(set(l2)), so basically it's an intersection operation here.



                I'm also using builtin zip() function to aggregate elements from each of the iterables ( both lists ).







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 21:21









                rockikz

                148110




                148110



























                    draft saved

                    draft discarded
















































                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


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




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252994%2fcompare-dictionary-values-that-belongs-to-different-keys%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







                    這個網誌中的熱門文章

                    What does pagestruct do in Eviews?

                    Dutch intervention in Lombok and Karangasem

                    Channel Islands