How to go through a nested for loop?
Hello I am trying to make my code go through a nested for loop, but the loop refuses to follow my original construct of thought.
My code is shown below.
def couple(men_choice, women_choice):
possible_engagements =
# men's first choice
for man in range(len(men_choice)):
for woman in men_choice:
pair = (man, woman[0])
possible_engagements.append(pair)
return possible_engagements
I am trying to design the first step of gale shapley algorithm, where each men will get paired with the first choice of woman in each of their list.
For example, if I have
>>> men_choice = [[1, 2, 3, 0], [3, 1, 2, 0], [2, 1, 3, 0], [3, 2, 0, 1]]
possible_engagements = [(0, 1), (0, 3), (0, 2), (0, 3)] # current output
possible_engagements = [(0, 1), (1, 3), (2, 2), (3, 3)] # desired output
Men's first choice of women are being outputted as I planned, but the index of men are not in sequence.
What problems do I have with my loop?
python
add a comment |
Hello I am trying to make my code go through a nested for loop, but the loop refuses to follow my original construct of thought.
My code is shown below.
def couple(men_choice, women_choice):
possible_engagements =
# men's first choice
for man in range(len(men_choice)):
for woman in men_choice:
pair = (man, woman[0])
possible_engagements.append(pair)
return possible_engagements
I am trying to design the first step of gale shapley algorithm, where each men will get paired with the first choice of woman in each of their list.
For example, if I have
>>> men_choice = [[1, 2, 3, 0], [3, 1, 2, 0], [2, 1, 3, 0], [3, 2, 0, 1]]
possible_engagements = [(0, 1), (0, 3), (0, 2), (0, 3)] # current output
possible_engagements = [(0, 1), (1, 3), (2, 2), (3, 3)] # desired output
Men's first choice of women are being outputted as I planned, but the index of men are not in sequence.
What problems do I have with my loop?
python
2
fix your indentation.
– Julien
Nov 13 '18 at 3:19
3
I've heard people speak that way about their code before - frustrated that it won't do what you tell it to do. The contrary is true - it will always do exactly what you tell it to do, you just have to learn to speak its language.
– Kind Stranger
Nov 13 '18 at 3:32
add a comment |
Hello I am trying to make my code go through a nested for loop, but the loop refuses to follow my original construct of thought.
My code is shown below.
def couple(men_choice, women_choice):
possible_engagements =
# men's first choice
for man in range(len(men_choice)):
for woman in men_choice:
pair = (man, woman[0])
possible_engagements.append(pair)
return possible_engagements
I am trying to design the first step of gale shapley algorithm, where each men will get paired with the first choice of woman in each of their list.
For example, if I have
>>> men_choice = [[1, 2, 3, 0], [3, 1, 2, 0], [2, 1, 3, 0], [3, 2, 0, 1]]
possible_engagements = [(0, 1), (0, 3), (0, 2), (0, 3)] # current output
possible_engagements = [(0, 1), (1, 3), (2, 2), (3, 3)] # desired output
Men's first choice of women are being outputted as I planned, but the index of men are not in sequence.
What problems do I have with my loop?
python
Hello I am trying to make my code go through a nested for loop, but the loop refuses to follow my original construct of thought.
My code is shown below.
def couple(men_choice, women_choice):
possible_engagements =
# men's first choice
for man in range(len(men_choice)):
for woman in men_choice:
pair = (man, woman[0])
possible_engagements.append(pair)
return possible_engagements
I am trying to design the first step of gale shapley algorithm, where each men will get paired with the first choice of woman in each of their list.
For example, if I have
>>> men_choice = [[1, 2, 3, 0], [3, 1, 2, 0], [2, 1, 3, 0], [3, 2, 0, 1]]
possible_engagements = [(0, 1), (0, 3), (0, 2), (0, 3)] # current output
possible_engagements = [(0, 1), (1, 3), (2, 2), (3, 3)] # desired output
Men's first choice of women are being outputted as I planned, but the index of men are not in sequence.
What problems do I have with my loop?
python
python
edited Nov 13 '18 at 3:24
eyllanesc
74.4k103156
74.4k103156
asked Nov 13 '18 at 3:13
V Anon
2066
2066
2
fix your indentation.
– Julien
Nov 13 '18 at 3:19
3
I've heard people speak that way about their code before - frustrated that it won't do what you tell it to do. The contrary is true - it will always do exactly what you tell it to do, you just have to learn to speak its language.
– Kind Stranger
Nov 13 '18 at 3:32
add a comment |
2
fix your indentation.
– Julien
Nov 13 '18 at 3:19
3
I've heard people speak that way about their code before - frustrated that it won't do what you tell it to do. The contrary is true - it will always do exactly what you tell it to do, you just have to learn to speak its language.
– Kind Stranger
Nov 13 '18 at 3:32
2
2
fix your indentation.
– Julien
Nov 13 '18 at 3:19
fix your indentation.
– Julien
Nov 13 '18 at 3:19
3
3
I've heard people speak that way about their code before - frustrated that it won't do what you tell it to do. The contrary is true - it will always do exactly what you tell it to do, you just have to learn to speak its language.
– Kind Stranger
Nov 13 '18 at 3:32
I've heard people speak that way about their code before - frustrated that it won't do what you tell it to do. The contrary is true - it will always do exactly what you tell it to do, you just have to learn to speak its language.
– Kind Stranger
Nov 13 '18 at 3:32
add a comment |
3 Answers
3
active
oldest
votes
As a list comprehension:
def couple(men_choice, women_choice):
return [(i, x[0]) for i, x in enumerate(men_choice)]
As a for-loop generator:
def couple(men_choice, women_choice):
for i, x in enumerate(men_choice):
yield (i, x[0])
As a for-loop + list.append:
def couple(men_choice, women_choice):
engagements =
for i, x in enumerate(men_choice):
engagements.append((i, x[0]))
return engagements
add a comment |
You only need one for loop to go through the mens choices, and to make sure you have no duplicate matchings, you have to check if the woman is already paired with another man.
def couple(men_choice, women_choice):
possible_engagements =
# men's first choice
for man in range(len(men_choice)):
i = 0
pair = (man, men_choice[i])
while ([x[1] for x in possible_engagements].count(men_choice[i]) > 0): #Check if woman is already chosen
pair = (man, men_choice[i])
i=i+1
possible_engagements.append(pair)
return possible_engagements
add a comment |
Your return keyword is located inside your outer loop. This means that man will only take the value 0, hence your current output. The code below achieves your desired output.
men_choice = [[1, 2, 3, 0], [3, 1, 2, 0], [2, 1, 3, 0], [3, 2, 0, 1]]
women_choice =
def couple(men_choice, women_choice):
possible_engagements =
for man in range(len(men_choice)):
possible_engagements.append((man, men_choice[man][0]))
return possible_engagements
possible_engagements = couple(men_choice, women_choice)
print(possible_engagements)
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%2f53273252%2fhow-to-go-through-a-nested-for-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
As a list comprehension:
def couple(men_choice, women_choice):
return [(i, x[0]) for i, x in enumerate(men_choice)]
As a for-loop generator:
def couple(men_choice, women_choice):
for i, x in enumerate(men_choice):
yield (i, x[0])
As a for-loop + list.append:
def couple(men_choice, women_choice):
engagements =
for i, x in enumerate(men_choice):
engagements.append((i, x[0]))
return engagements
add a comment |
As a list comprehension:
def couple(men_choice, women_choice):
return [(i, x[0]) for i, x in enumerate(men_choice)]
As a for-loop generator:
def couple(men_choice, women_choice):
for i, x in enumerate(men_choice):
yield (i, x[0])
As a for-loop + list.append:
def couple(men_choice, women_choice):
engagements =
for i, x in enumerate(men_choice):
engagements.append((i, x[0]))
return engagements
add a comment |
As a list comprehension:
def couple(men_choice, women_choice):
return [(i, x[0]) for i, x in enumerate(men_choice)]
As a for-loop generator:
def couple(men_choice, women_choice):
for i, x in enumerate(men_choice):
yield (i, x[0])
As a for-loop + list.append:
def couple(men_choice, women_choice):
engagements =
for i, x in enumerate(men_choice):
engagements.append((i, x[0]))
return engagements
As a list comprehension:
def couple(men_choice, women_choice):
return [(i, x[0]) for i, x in enumerate(men_choice)]
As a for-loop generator:
def couple(men_choice, women_choice):
for i, x in enumerate(men_choice):
yield (i, x[0])
As a for-loop + list.append:
def couple(men_choice, women_choice):
engagements =
for i, x in enumerate(men_choice):
engagements.append((i, x[0]))
return engagements
answered Nov 13 '18 at 3:31
Mateen Ulhaq
11.3k114691
11.3k114691
add a comment |
add a comment |
You only need one for loop to go through the mens choices, and to make sure you have no duplicate matchings, you have to check if the woman is already paired with another man.
def couple(men_choice, women_choice):
possible_engagements =
# men's first choice
for man in range(len(men_choice)):
i = 0
pair = (man, men_choice[i])
while ([x[1] for x in possible_engagements].count(men_choice[i]) > 0): #Check if woman is already chosen
pair = (man, men_choice[i])
i=i+1
possible_engagements.append(pair)
return possible_engagements
add a comment |
You only need one for loop to go through the mens choices, and to make sure you have no duplicate matchings, you have to check if the woman is already paired with another man.
def couple(men_choice, women_choice):
possible_engagements =
# men's first choice
for man in range(len(men_choice)):
i = 0
pair = (man, men_choice[i])
while ([x[1] for x in possible_engagements].count(men_choice[i]) > 0): #Check if woman is already chosen
pair = (man, men_choice[i])
i=i+1
possible_engagements.append(pair)
return possible_engagements
add a comment |
You only need one for loop to go through the mens choices, and to make sure you have no duplicate matchings, you have to check if the woman is already paired with another man.
def couple(men_choice, women_choice):
possible_engagements =
# men's first choice
for man in range(len(men_choice)):
i = 0
pair = (man, men_choice[i])
while ([x[1] for x in possible_engagements].count(men_choice[i]) > 0): #Check if woman is already chosen
pair = (man, men_choice[i])
i=i+1
possible_engagements.append(pair)
return possible_engagements
You only need one for loop to go through the mens choices, and to make sure you have no duplicate matchings, you have to check if the woman is already paired with another man.
def couple(men_choice, women_choice):
possible_engagements =
# men's first choice
for man in range(len(men_choice)):
i = 0
pair = (man, men_choice[i])
while ([x[1] for x in possible_engagements].count(men_choice[i]) > 0): #Check if woman is already chosen
pair = (man, men_choice[i])
i=i+1
possible_engagements.append(pair)
return possible_engagements
answered Nov 13 '18 at 3:21
Abhishek Patel
349419
349419
add a comment |
add a comment |
Your return keyword is located inside your outer loop. This means that man will only take the value 0, hence your current output. The code below achieves your desired output.
men_choice = [[1, 2, 3, 0], [3, 1, 2, 0], [2, 1, 3, 0], [3, 2, 0, 1]]
women_choice =
def couple(men_choice, women_choice):
possible_engagements =
for man in range(len(men_choice)):
possible_engagements.append((man, men_choice[man][0]))
return possible_engagements
possible_engagements = couple(men_choice, women_choice)
print(possible_engagements)
add a comment |
Your return keyword is located inside your outer loop. This means that man will only take the value 0, hence your current output. The code below achieves your desired output.
men_choice = [[1, 2, 3, 0], [3, 1, 2, 0], [2, 1, 3, 0], [3, 2, 0, 1]]
women_choice =
def couple(men_choice, women_choice):
possible_engagements =
for man in range(len(men_choice)):
possible_engagements.append((man, men_choice[man][0]))
return possible_engagements
possible_engagements = couple(men_choice, women_choice)
print(possible_engagements)
add a comment |
Your return keyword is located inside your outer loop. This means that man will only take the value 0, hence your current output. The code below achieves your desired output.
men_choice = [[1, 2, 3, 0], [3, 1, 2, 0], [2, 1, 3, 0], [3, 2, 0, 1]]
women_choice =
def couple(men_choice, women_choice):
possible_engagements =
for man in range(len(men_choice)):
possible_engagements.append((man, men_choice[man][0]))
return possible_engagements
possible_engagements = couple(men_choice, women_choice)
print(possible_engagements)
Your return keyword is located inside your outer loop. This means that man will only take the value 0, hence your current output. The code below achieves your desired output.
men_choice = [[1, 2, 3, 0], [3, 1, 2, 0], [2, 1, 3, 0], [3, 2, 0, 1]]
women_choice =
def couple(men_choice, women_choice):
possible_engagements =
for man in range(len(men_choice)):
possible_engagements.append((man, men_choice[man][0]))
return possible_engagements
possible_engagements = couple(men_choice, women_choice)
print(possible_engagements)
answered Nov 13 '18 at 3:29
Patol75
6236
6236
add a comment |
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.
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.
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%2f53273252%2fhow-to-go-through-a-nested-for-loop%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
2
fix your indentation.
– Julien
Nov 13 '18 at 3:19
3
I've heard people speak that way about their code before - frustrated that it won't do what you tell it to do. The contrary is true - it will always do exactly what you tell it to do, you just have to learn to speak its language.
– Kind Stranger
Nov 13 '18 at 3:32