python 我不能添加从查询sql中获取的列表中的元素,并在循环中为字典中的每个对应元素打印它们

kfgdxczn  于 2023-01-16  发布在  Python
关注(0)|答案(1)|浏览(107)

我不能把从db2查询中得到的元素添加到列表中,然后在循环中为字典中的每个对应元素打印它们,例如,结果如下:{'Minnesota-Chicago': ['NHL', 8.1, 9, '15:00', [0, 1, 1, 5], 0, 0, 0, 3],但是i我想把0,0,0,3放在括号里:[0、0、0、3]
让我们按顺序进行:
1.我有一个由x[row[0]][4].append(row[5])组成的字典。这是之前创建的。一切都是正确的。结果是一个循环,其中每个五线谱由以下内容组成:{'Minnesota-Chicago': ['NHL', 8.1, 9, '15:00', [0, 1, 1, 5]
1.现在,我想向字典x添加另一个值,但这个新值在另一个不同的查询(db2)中。这个新值也称为row[5](score_away),但它与前一行[5]中的值不同。

db2 = cursor_test.execute('''SELECT Next.team_home||"-"||Next.team_away, Next.tournmant,
                                           Next.date, Next.round, Next.time,
                                           Results.score_away
                                    FROM Next
                                    INNER JOIN Results
                                    ON Next.team_home = Results.team_home;''')

for row in db2.fetchall():
    x[row[0]].append(row[5])

print(x)
  1. Row[5](score_away)不是列表[0,0,0,3],而是循环中该列表的每个数字(例如0,0,0,3,2,6,1,2 ...)。我使用append在row [5]和key(即row [0])之间添加它。我得到以下输出:
{'Minnesota-Chicago': ['NHL', 8.1, 9, '15:00', [0, 1, 1, 5], 0, 0, 0, 3]

相反,我希望得到以下输出:

{'Minnesota-Chicago': ['NHL', 8.1, 9, '15:00', [0, 1, 1, 5], [0, 0, 0, 3]
    • 这是完整的代码**
db1 = cursor_test.execute('''SELECT Next.team_home||"-"||Next.team_away, Next.tournmant,
                                                  Next.date, Next.round, Next.time,
                                                  Results.score_home
                                           FROM Next
                                           INNER JOIN Results
                                           ON Next.team_home = Results.team_home;''')
    

   for row in db1.fetchall():
       if row[0] is not in x:

           x[row[0]] = list(row[1:])
           x[row[0]][4] = [x[row[0]][4]] # transform int into list of int
       other:
           x[row[0]][4].append(row[5])

   # ADD SCORE
   db2 = cursor_test.execute('''SELECT Next.team_home||"-"||Next.team_away, Next.tournmant,
                                              Next.date, Next.round, Next.time,
                                              Results.score_away
                                       FROM Next
                                       INNER JOIN Results
                                       ON Next.team_home = Results.team_home;''')

   for row in db2.fetchall():
       x[row[0]].append(row[5])

   print(x)

谢谢你的帮忙!

o8x7eapl

o8x7eapl1#

for row in db2.fetchall():
       x[row[0]].append(row[5])

现在的情况是你把客场得分附加到列表中,而不是把它们累积到列表中,然后再把它们附加到列表中。这很难理解,因为这是一个有意义的值列表,而不是一个对象。下面是我建议你做的。
创建一个class来保存信息,而不是使用列表。

from dataclasses import dataclass

@dataclass
class GameInfo:
    tournament: str
    date: float
    round: int
    time: str
    home_score: list[int]
    away_score: list[int]

现在有了hold info的类,我们可以编写代码来构建dict

db1 = cursor_test.execute('''SELECT Next.team_home||"-"||Next.team_away, Next.tournmant,
                                                  Next.date, Next.round, Next.time,
                                                  Results.score_home
                                           FROM Next
                                           INNER JOIN Results
                                           ON Next.team_home = Results.team_home;''')

for row in db1.fetchall():
    if row[0] not in x:
        info = GameInfo(
            tournament=row[1],
            date=row[2],
            round=row[3],
            time=row[4],
            away_score=list(),
            home_score=list()
        )
        x[row[0]] = info
        
    x[row[0]].home_score.append(row[5])

db2 = cursor_test.execute('''SELECT Next.team_home||"-"||Next.team_away, Next.tournmant,
                                              Next.date, Next.round, Next.time,
                                              Results.score_away
                                       FROM Next
                                       INNER JOIN Results
                                       ON Next.team_home = Results.team_home;''')

for row in db2.fetchall():
    if row[0] not in x:
        info = GameInfo(
            tournament=row[1],
            date=row[2],
            round=row[3],
            time=row[4],
            away_score=list(),
            home_score=list()
        )
        x[row[0]] = info

    x[row[0]].away_score.append(row[5])

print(x)

相关问题