comparing the second element of tuples in a list











up vote
0
down vote

favorite












I am currently writing a program that demonstrates the Gale-Shapley algorithm.



Description of Gale-Shapley algorithm on wikipedia



I have two lists as input.



preference_boys, which is the boys' preferences on the girls and



preference_girls, which is the girls' preferences on the boys.



In the code, the first ones to choose are the boys and I have successfully managed to pair each boy



with his first choice of the girls.



'''
preference_boys = [[2, 0, 1, 3], [1, 2, 3, 0], [0, 3, 1, 2], [2, 3, 0, 1]]

preference_girls = [[0, 2, 1, 3], [2, 3, 0, 1], [3, 1, 2, 0], [0, 3, 1, 2]]
'''

def couple_maker(preference_boys, preference_girls):
# stores the couples based on boys first choice of girls
temporary_couples =

single_boys =

for boy in range(len(preference_boys)):
single_boys.append(boy)
# single_boys = [0, 1, 2, 3]


while len(single_boys) > 0:
for single_boy in single_boys:
for girl in preference_boys[single_boy]:
temporary_couple = (single_boy, girl)
single_boys.remove(single_boy)
temporary_couples.append(temporary_couple)
break


return temporary_couples

>>> couple_maker(preference_boys, preference_girls)
[(0, 2), (2, 0), (1, 1), (3, 2)]


Now, I need to make another condition to solve the case when two couples share a single girl.



From my current output of temporary_couples (0, 2) and (3, 2) couples share the same girl.



What methods can I use to compare the preferences of the overlapping girl(the second element



of the tuple in the list) and remove the couple that has the boy with a lower priority to the girl?










share|improve this question
























  • @Ayxan for list preference_boys, the elements in the nested lists are each boy's preference of the girls from highest to lowest. for list preference_girls, the nested lists are each girl's preference of the boys. Since boys are first ones to choose and assuming that they all grab the girl of their first choice, from preference boys I can have [(0,2), (1,1), (2,0), (3,2)]. My problem is dealing with the shared girl which is physically impossible to happen for the girl.
    – V Anon
    Nov 17 at 5:57

















up vote
0
down vote

favorite












I am currently writing a program that demonstrates the Gale-Shapley algorithm.



Description of Gale-Shapley algorithm on wikipedia



I have two lists as input.



preference_boys, which is the boys' preferences on the girls and



preference_girls, which is the girls' preferences on the boys.



In the code, the first ones to choose are the boys and I have successfully managed to pair each boy



with his first choice of the girls.



'''
preference_boys = [[2, 0, 1, 3], [1, 2, 3, 0], [0, 3, 1, 2], [2, 3, 0, 1]]

preference_girls = [[0, 2, 1, 3], [2, 3, 0, 1], [3, 1, 2, 0], [0, 3, 1, 2]]
'''

def couple_maker(preference_boys, preference_girls):
# stores the couples based on boys first choice of girls
temporary_couples =

single_boys =

for boy in range(len(preference_boys)):
single_boys.append(boy)
# single_boys = [0, 1, 2, 3]


while len(single_boys) > 0:
for single_boy in single_boys:
for girl in preference_boys[single_boy]:
temporary_couple = (single_boy, girl)
single_boys.remove(single_boy)
temporary_couples.append(temporary_couple)
break


return temporary_couples

>>> couple_maker(preference_boys, preference_girls)
[(0, 2), (2, 0), (1, 1), (3, 2)]


Now, I need to make another condition to solve the case when two couples share a single girl.



From my current output of temporary_couples (0, 2) and (3, 2) couples share the same girl.



What methods can I use to compare the preferences of the overlapping girl(the second element



of the tuple in the list) and remove the couple that has the boy with a lower priority to the girl?










share|improve this question
























  • @Ayxan for list preference_boys, the elements in the nested lists are each boy's preference of the girls from highest to lowest. for list preference_girls, the nested lists are each girl's preference of the boys. Since boys are first ones to choose and assuming that they all grab the girl of their first choice, from preference boys I can have [(0,2), (1,1), (2,0), (3,2)]. My problem is dealing with the shared girl which is physically impossible to happen for the girl.
    – V Anon
    Nov 17 at 5:57















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am currently writing a program that demonstrates the Gale-Shapley algorithm.



Description of Gale-Shapley algorithm on wikipedia



I have two lists as input.



preference_boys, which is the boys' preferences on the girls and



preference_girls, which is the girls' preferences on the boys.



In the code, the first ones to choose are the boys and I have successfully managed to pair each boy



with his first choice of the girls.



'''
preference_boys = [[2, 0, 1, 3], [1, 2, 3, 0], [0, 3, 1, 2], [2, 3, 0, 1]]

preference_girls = [[0, 2, 1, 3], [2, 3, 0, 1], [3, 1, 2, 0], [0, 3, 1, 2]]
'''

def couple_maker(preference_boys, preference_girls):
# stores the couples based on boys first choice of girls
temporary_couples =

single_boys =

for boy in range(len(preference_boys)):
single_boys.append(boy)
# single_boys = [0, 1, 2, 3]


while len(single_boys) > 0:
for single_boy in single_boys:
for girl in preference_boys[single_boy]:
temporary_couple = (single_boy, girl)
single_boys.remove(single_boy)
temporary_couples.append(temporary_couple)
break


return temporary_couples

>>> couple_maker(preference_boys, preference_girls)
[(0, 2), (2, 0), (1, 1), (3, 2)]


Now, I need to make another condition to solve the case when two couples share a single girl.



From my current output of temporary_couples (0, 2) and (3, 2) couples share the same girl.



What methods can I use to compare the preferences of the overlapping girl(the second element



of the tuple in the list) and remove the couple that has the boy with a lower priority to the girl?










share|improve this question















I am currently writing a program that demonstrates the Gale-Shapley algorithm.



Description of Gale-Shapley algorithm on wikipedia



I have two lists as input.



preference_boys, which is the boys' preferences on the girls and



preference_girls, which is the girls' preferences on the boys.



In the code, the first ones to choose are the boys and I have successfully managed to pair each boy



with his first choice of the girls.



'''
preference_boys = [[2, 0, 1, 3], [1, 2, 3, 0], [0, 3, 1, 2], [2, 3, 0, 1]]

preference_girls = [[0, 2, 1, 3], [2, 3, 0, 1], [3, 1, 2, 0], [0, 3, 1, 2]]
'''

def couple_maker(preference_boys, preference_girls):
# stores the couples based on boys first choice of girls
temporary_couples =

single_boys =

for boy in range(len(preference_boys)):
single_boys.append(boy)
# single_boys = [0, 1, 2, 3]


while len(single_boys) > 0:
for single_boy in single_boys:
for girl in preference_boys[single_boy]:
temporary_couple = (single_boy, girl)
single_boys.remove(single_boy)
temporary_couples.append(temporary_couple)
break


return temporary_couples

>>> couple_maker(preference_boys, preference_girls)
[(0, 2), (2, 0), (1, 1), (3, 2)]


Now, I need to make another condition to solve the case when two couples share a single girl.



From my current output of temporary_couples (0, 2) and (3, 2) couples share the same girl.



What methods can I use to compare the preferences of the overlapping girl(the second element



of the tuple in the list) and remove the couple that has the boy with a lower priority to the girl?







python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 6:00

























asked Nov 17 at 5:41









V Anon

2006




2006












  • @Ayxan for list preference_boys, the elements in the nested lists are each boy's preference of the girls from highest to lowest. for list preference_girls, the nested lists are each girl's preference of the boys. Since boys are first ones to choose and assuming that they all grab the girl of their first choice, from preference boys I can have [(0,2), (1,1), (2,0), (3,2)]. My problem is dealing with the shared girl which is physically impossible to happen for the girl.
    – V Anon
    Nov 17 at 5:57




















  • @Ayxan for list preference_boys, the elements in the nested lists are each boy's preference of the girls from highest to lowest. for list preference_girls, the nested lists are each girl's preference of the boys. Since boys are first ones to choose and assuming that they all grab the girl of their first choice, from preference boys I can have [(0,2), (1,1), (2,0), (3,2)]. My problem is dealing with the shared girl which is physically impossible to happen for the girl.
    – V Anon
    Nov 17 at 5:57


















@Ayxan for list preference_boys, the elements in the nested lists are each boy's preference of the girls from highest to lowest. for list preference_girls, the nested lists are each girl's preference of the boys. Since boys are first ones to choose and assuming that they all grab the girl of their first choice, from preference boys I can have [(0,2), (1,1), (2,0), (3,2)]. My problem is dealing with the shared girl which is physically impossible to happen for the girl.
– V Anon
Nov 17 at 5:57






@Ayxan for list preference_boys, the elements in the nested lists are each boy's preference of the girls from highest to lowest. for list preference_girls, the nested lists are each girl's preference of the boys. Since boys are first ones to choose and assuming that they all grab the girl of their first choice, from preference boys I can have [(0,2), (1,1), (2,0), (3,2)]. My problem is dealing with the shared girl which is physically impossible to happen for the girl.
– V Anon
Nov 17 at 5:57














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










One way, would be to group the temporary_couples "by girl" (the 1th index), using something like a defaultdict:



from collections import defaultdict

temp_couples = [(0, 2), (2, 0), (1, 1), (3, 2)]

by_girl = defaultdict(list)

for (b,g) in temp_couples:
by_girl[g].append((b,g))

for (g,her_temp_couples) in by_girl.items():
if len(her_temp_couples) > 1:
print("Girl", g, "has conflicts:", her_temp_couples)


Output:



Girl 2 has conflicts: [(0, 2), (3, 2)]


Then you could go on to resolve the same way the algorithm you cited does.



For clarity, after it's populated in the first loop, by_girl looks like:



{
0: [(2, 0)],
1: [(1, 1)]
2: [(0, 2), (3, 2)],
3: # Implicitly
}





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%2f53348576%2fcomparing-the-second-element-of-tuples-in-a-list%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










    One way, would be to group the temporary_couples "by girl" (the 1th index), using something like a defaultdict:



    from collections import defaultdict

    temp_couples = [(0, 2), (2, 0), (1, 1), (3, 2)]

    by_girl = defaultdict(list)

    for (b,g) in temp_couples:
    by_girl[g].append((b,g))

    for (g,her_temp_couples) in by_girl.items():
    if len(her_temp_couples) > 1:
    print("Girl", g, "has conflicts:", her_temp_couples)


    Output:



    Girl 2 has conflicts: [(0, 2), (3, 2)]


    Then you could go on to resolve the same way the algorithm you cited does.



    For clarity, after it's populated in the first loop, by_girl looks like:



    {
    0: [(2, 0)],
    1: [(1, 1)]
    2: [(0, 2), (3, 2)],
    3: # Implicitly
    }





    share|improve this answer

























      up vote
      1
      down vote



      accepted










      One way, would be to group the temporary_couples "by girl" (the 1th index), using something like a defaultdict:



      from collections import defaultdict

      temp_couples = [(0, 2), (2, 0), (1, 1), (3, 2)]

      by_girl = defaultdict(list)

      for (b,g) in temp_couples:
      by_girl[g].append((b,g))

      for (g,her_temp_couples) in by_girl.items():
      if len(her_temp_couples) > 1:
      print("Girl", g, "has conflicts:", her_temp_couples)


      Output:



      Girl 2 has conflicts: [(0, 2), (3, 2)]


      Then you could go on to resolve the same way the algorithm you cited does.



      For clarity, after it's populated in the first loop, by_girl looks like:



      {
      0: [(2, 0)],
      1: [(1, 1)]
      2: [(0, 2), (3, 2)],
      3: # Implicitly
      }





      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        One way, would be to group the temporary_couples "by girl" (the 1th index), using something like a defaultdict:



        from collections import defaultdict

        temp_couples = [(0, 2), (2, 0), (1, 1), (3, 2)]

        by_girl = defaultdict(list)

        for (b,g) in temp_couples:
        by_girl[g].append((b,g))

        for (g,her_temp_couples) in by_girl.items():
        if len(her_temp_couples) > 1:
        print("Girl", g, "has conflicts:", her_temp_couples)


        Output:



        Girl 2 has conflicts: [(0, 2), (3, 2)]


        Then you could go on to resolve the same way the algorithm you cited does.



        For clarity, after it's populated in the first loop, by_girl looks like:



        {
        0: [(2, 0)],
        1: [(1, 1)]
        2: [(0, 2), (3, 2)],
        3: # Implicitly
        }





        share|improve this answer












        One way, would be to group the temporary_couples "by girl" (the 1th index), using something like a defaultdict:



        from collections import defaultdict

        temp_couples = [(0, 2), (2, 0), (1, 1), (3, 2)]

        by_girl = defaultdict(list)

        for (b,g) in temp_couples:
        by_girl[g].append((b,g))

        for (g,her_temp_couples) in by_girl.items():
        if len(her_temp_couples) > 1:
        print("Girl", g, "has conflicts:", her_temp_couples)


        Output:



        Girl 2 has conflicts: [(0, 2), (3, 2)]


        Then you could go on to resolve the same way the algorithm you cited does.



        For clarity, after it's populated in the first loop, by_girl looks like:



        {
        0: [(2, 0)],
        1: [(1, 1)]
        2: [(0, 2), (3, 2)],
        3: # Implicitly
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 17 at 5:50









        jedwards

        20.8k3158




        20.8k3158






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348576%2fcomparing-the-second-element-of-tuples-in-a-list%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