如何通过嵌套的 for 循环?
How to go through a nested for loop?
您好,我正在尝试让我的代码通过嵌套的 for 循环,但该循环拒绝遵循我最初的想法。
我的代码如下所示。
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
我正在尝试设计 gale shapley 算法的第一步,其中每个男人都会与他们每个列表中的第一选择的女人配对。
例如,如果我有
>>> 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
按照我的计划输出了男性对女性的首选,但是男性的索引没有顺序。
我的循环有什么问题?
你只需要一个 for 循环来完成男士的选择,并且为了确保你没有重复匹配,你必须检查女士是否已经与另一个男士配对。
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
您的 return 关键字位于外循环内。这意味着 man 只会取值 0,因此您的当前输出。下面的代码实现了您想要的输出。
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)
作为列表理解:
def couple(men_choice, women_choice):
return [(i, x[0]) for i, x in enumerate(men_choice)]
作为for循环生成器:
def couple(men_choice, women_choice):
for i, x in enumerate(men_choice):
yield (i, x[0])
作为 for 循环 + list.append:
def couple(men_choice, women_choice):
engagements = []
for i, x in enumerate(men_choice):
engagements.append((i, x[0]))
return engagements
您好,我正在尝试让我的代码通过嵌套的 for 循环,但该循环拒绝遵循我最初的想法。
我的代码如下所示。
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
我正在尝试设计 gale shapley 算法的第一步,其中每个男人都会与他们每个列表中的第一选择的女人配对。
例如,如果我有
>>> 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
按照我的计划输出了男性对女性的首选,但是男性的索引没有顺序。
我的循环有什么问题?
你只需要一个 for 循环来完成男士的选择,并且为了确保你没有重复匹配,你必须检查女士是否已经与另一个男士配对。
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
您的 return 关键字位于外循环内。这意味着 man 只会取值 0,因此您的当前输出。下面的代码实现了您想要的输出。
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)
作为列表理解:
def couple(men_choice, women_choice):
return [(i, x[0]) for i, x in enumerate(men_choice)]
作为for循环生成器:
def couple(men_choice, women_choice):
for i, x in enumerate(men_choice):
yield (i, x[0])
作为 for 循环 + list.append:
def couple(men_choice, women_choice):
engagements = []
for i, x in enumerate(men_choice):
engagements.append((i, x[0]))
return engagements