Home > OS >  I can't extract items in a loop correctly. I extract only the last single number and not the wh
I can't extract items in a loop correctly. I extract only the last single number and not the wh

Time:12-27

If i write HockeyMatch[row[0]].append(row[3]) i correctly get the list of hockey matches with their last points won, for example Chicago-Minnesota [4, 2, 0, 3], because row[0] is the name of the hockey game and row[3] is the list of last games points won by Chicago.

My goal is to also add the tournament name (it's row[1]) and date (it's row[2]) to it, so I get this:

{('Chicago-Minnesota', 'NHL', 12/27/ 2022): [4, 2, 0, 3]

I tried to do this, writing like this:

HockeyMatch[ row[0],row[1],row[2] ].append(row[3])

The problem though is i get this Chicago-Minnesota, NHL, 27.12.2022, [4]. As you can see, [4] is the last point conquered in the list [4, 2, 0, 3].

Instead if I try to write HockeyMatch[row[0]].append(row[3]), I get correctly [4, 2, 0, 3], but obviously without date and tournament, so Chicago-Minnesota [4, 2, 0, 3]

How can I correctly print the list of games with date (is row[2]), tournament (is row[1]) and last points won (is row[3])? I would like to get this {('Chicago-Minnesota', 'NHL', 12/27/2022): [4, 2, 0, 3] (and other hockey matchs)

HockeyMatch = {}

conn_test = sqlite3.connect('database.db')
cursor_test = conn_test.cursor()
my = cursor_test.execute('''SELECT Next.ClubHome||"-"||Next.ClubAway, Next.Tournament, Next.Date, MatchResults.ScoreHome
                                    FROM Next
                                    INNER JOIN MatchResults 
                                    ON Next.ClubHome = MatchResults.ClubHome;''') 

for row in my.fetchall():
    if row[0] not in HockeyMatch:

        HockeyMatch[ row[0],row[1],row[2] ] = []
    HockeyMatch[ row[0],row[1],row[2] ].append(row[3])
    print(HockeyMatch)

Example database

NEXT

| ClubHome | ClubAway   | Tournament | Date |
---------------------------------------------
| Chicago  | Minnesota  | NHL        | 27.12|
| Arizona  | Los Angeles| NHL        | 27.12|
| Dallas   | Vegas Gold | NHL        | 27.12|

MATCHRESULTS

To connect to the example in the question, here I only select Chicago who are in ClubHome and select their points. I'm not interested in ClubAway

| ClubHome | ClubAway   | Tournament | Date | ScoreHome | ScoreAway
--------------------------------------------------------------------
| Chicago  | Toronto    | NHL        | 27.12| 1         | 2 
| New York | Vegas Gold | NHL        | 27.12| 2         | 3
| Dallas   | Minnesota  | NHL        | 27.12| 0         | 1
| Chicago  | Buffalo Sab| NHL        | 27.12| 2         | 0

and other matchs...

CodePudding user response:

Change

    if row[0] not in HockeyMatch:

to

    if (row[0], row[1],row[2]) not in HockeyMatch:

When you change the key to a tuple, you have to search for the tuple with in.

You can simplify this by using collections.defaultdict() or dict.setdefault().

for row in data:
    HockeyMatch.setdefault((row[0],row[1],row[2]), []).append(row[3])
    print(HockeyMatch)
  • Related