首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在PSQL中查询表,使其返回与pairs相同的列表?

如何在PSQL中查询表,使其返回与pairs相同的列表?
EN

Stack Overflow用户
提问于 2017-10-20 05:34:44
回答 1查看 26关注 0票数 1

我有一份锦标赛参赛选手名单。

代码语言:javascript
复制
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做到这一点?

代码语言:javascript
复制
       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)
EN

回答 1

Stack Overflow用户

发布于 2017-10-20 13:52:02

解决了!不幸的是,我无法找到一种使用简单的SQL查询来实现这一点的方法。不过,多亏了Python上的一个线程,我才能够使用StackOverFlow的itertools找到解决方案。它现在返回4对,而不是28对。我已经通过了全部10个测试!以下是我添加到tournament.py中的内容:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46839253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档