我有一份锦标赛参赛选手名单。
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)发布于 2017-10-20 13:52:02
解决了!不幸的是,我无法找到一种使用简单的SQL查询来实现这一点的方法。不过,多亏了Python上的一个线程,我才能够使用StackOverFlow的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 resultshttps://stackoverflow.com/questions/46839253
复制相似问题