我有锦标赛中的球员名单。
tournament=> SELECT * FROM players;
name | player_id
-------------------+-----------
Twilight Sparkle | 9
Fluttershy | 10
Applejack | 11
Pinkie Pie | 12
Rarity | 13
Rainbow Dash | 14
Princess Celestia | 15
Princess Luna | 16
(8 rows)
这就是我希望列表的样子。我如何要求postgreSQL这样做?
name1 | id1 | name2 | id2
-------------------+---------+-------------------+-------
Twilight Sparkle | 9 | Fluttershy | 10
Applejack | 11 | Pinkie Pie | 12
Rarity | 13 | Rainbow Dash | 14
Princess Celestia | 15 | Princess Luna | 16
(4 pairs)
答案 0 :(得分:0)
解决!不幸的是,我找不到用简单的SQL查询来做到这一点的方法。但是,感谢StackOverFlow here上的一个线程,我能够使用Python的itertools找到解决方案。它现在返回4对而不是28对。我通过了所有10次测试!以下是我对tournament.py的补充:
import itertools
swissPairings():
standings = playerStandings()
pairingsiterator = itertools.izip(*[iter(standings)]*2)
# Iterate through the list and build the pairings
results = []
pairings = list(pairingsiterator)
for pair in pairings:
id1 = pair[0][0]
name1 = pair[0][1]
id2 = pair[1][0]
name2 = pair[1][1]
matchup = (id1, name1, id2, name2)
results.append(matchup)
return results