我列出了所有英超球队的名单:
teamlist = ["arsenal", "aston-villa", "bournemouth", "chelsea", "crystal-palace", "everton","leicester-city", "liverpool", "manchester-city", "manchester-united", "newcastle-united", "norwich-city", "southampton","stoke-city", "swansea-city", "tottenham-hotspur", "watford", "west-bromich-albion","west-ham-united" ]
我需要计算所有可能的team1-vs-team2配对.
目前我有以下代码:
oppo = 0 for team in teamlist: print team + "-vs-" + teamlist[oppo] oppo+=1 print team + "-vs-" + teamlist[oppo] oppo+=1
这将输出:
arsenal-vs-arsenal arsenal-vs-aston-villa
然而,我需要通过每个团队,显示他们所有可能的主场比赛装置,然后进入团队列表中的下一个团队,输出他们所有可能的主场比赛装置并重复直到所有球队完成.
嵌套for
循环的替代方法是从列表的项目计算长度为2的所有排列.
>>> from itertools import permutations >>> for team1, team2 in permutations(teamlist, 2): ... print '{0} -vs- {1}'.format(team1, team2) ... arsenal -vs- aston-villa arsenal -vs- bournemouth arsenal -vs- chelsea # and so on ...