为什么我的元组列表只有一个条目?
Why does my list of tuples only have one entry?
我有一个元组列表,用作函数的参数。我希望该函数随机选择列表中的一个元组(我使用的是 random.randint,但出于测试目的将其注释掉)和 return。如果列表中的每个条目都是一个元组而不是一个条目,我该如何更改它?
announce_winner()
工作正常。我就是没加进去。
def get_winner(*nominees):
# Should accept as nominees a list of items
winner = nominees[0]
#[random.randint(1, range(nominees))]
return winner
# Randomly pick one of the nominees (list items) as the winner
# Return the winner (list item) to the caller
def main():
best_original_score_list = [('Terence Blanchard','Da 5 Bloods'),
('Trent Reznor and Atticus Ross','Mank'),
('Emile Mosseri','Minari'),
('James Newton Howard','News of the World'),
('Trent Reznor, Atticus Ross, and Jon Batiste','Soul')]
announce_nominees('Best Original Score',best_original_score_list)
winner = get_winner(best_original_score_list)
main()
您已经很接近了,但如果我理解您要完成的目标,下面的代码可能会为您指明方向。具体来说,如果您想从 best_original_score_list
中随机选择一个元组,然后传入列表(无需使用 *nominees
对其进行“元组操作”),然后使用列表中的随机选择一个元组适当的随机函数。
示例:
from random import randrange
def get_winner(nominees):
winner = nominees[randrange(0, len(nominees))]
return winner
def main():
best_original_score_list = [('Terence Blanchard','Da 5 Bloods'),
('Trent Reznor and Atticus Ross','Mank'),
('Emile Mosseri','Minari'),
('James Newton Howard','News of the World'),
('Trent Reznor, Atticus Ross, and Jon Batiste','Soul')]
winner = get_winner(best_original_score_list)
print(winner)
main()
输出:
('Trent Reznor and Atticus Ross', 'Mank')
我相信你真的在看 random.choice
def get_winner(nominees):
return random.choice(nominees)
def main():
best_original_score_list = [
('Terence Blanchard','Da 5 Bloods'),
('Trent Reznor and Atticus Ross','Mank'),
('Emile Mosseri','Minari'),
('James Newton Howard','News of the World'),
('Trent Reznor, Atticus Ross, and Jon Batiste','Soul'),
]
announce_nominees('Best Original Score',best_original_score_list)
winner = get_winner(best_original_score_list)
main()
我有一个元组列表,用作函数的参数。我希望该函数随机选择列表中的一个元组(我使用的是 random.randint,但出于测试目的将其注释掉)和 return。如果列表中的每个条目都是一个元组而不是一个条目,我该如何更改它?
announce_winner()
工作正常。我就是没加进去。
def get_winner(*nominees):
# Should accept as nominees a list of items
winner = nominees[0]
#[random.randint(1, range(nominees))]
return winner
# Randomly pick one of the nominees (list items) as the winner
# Return the winner (list item) to the caller
def main():
best_original_score_list = [('Terence Blanchard','Da 5 Bloods'),
('Trent Reznor and Atticus Ross','Mank'),
('Emile Mosseri','Minari'),
('James Newton Howard','News of the World'),
('Trent Reznor, Atticus Ross, and Jon Batiste','Soul')]
announce_nominees('Best Original Score',best_original_score_list)
winner = get_winner(best_original_score_list)
main()
您已经很接近了,但如果我理解您要完成的目标,下面的代码可能会为您指明方向。具体来说,如果您想从 best_original_score_list
中随机选择一个元组,然后传入列表(无需使用 *nominees
对其进行“元组操作”),然后使用列表中的随机选择一个元组适当的随机函数。
示例:
from random import randrange
def get_winner(nominees):
winner = nominees[randrange(0, len(nominees))]
return winner
def main():
best_original_score_list = [('Terence Blanchard','Da 5 Bloods'),
('Trent Reznor and Atticus Ross','Mank'),
('Emile Mosseri','Minari'),
('James Newton Howard','News of the World'),
('Trent Reznor, Atticus Ross, and Jon Batiste','Soul')]
winner = get_winner(best_original_score_list)
print(winner)
main()
输出:
('Trent Reznor and Atticus Ross', 'Mank')
我相信你真的在看 random.choice
def get_winner(nominees):
return random.choice(nominees)
def main():
best_original_score_list = [
('Terence Blanchard','Da 5 Bloods'),
('Trent Reznor and Atticus Ross','Mank'),
('Emile Mosseri','Minari'),
('James Newton Howard','News of the World'),
('Trent Reznor, Atticus Ross, and Jon Batiste','Soul'),
]
announce_nominees('Best Original Score',best_original_score_list)
winner = get_winner(best_original_score_list)
main()