Monday, 23 January 2023

Duplication of a list, with append, at each click on the checkbox. How can i avoid duplication?

enter image description here

If i check the checkbox, it starts a function that prints the data, including score_home which is a list of numbers in a dictionary, for example (score_home=[0, 1, 1, 5]):

Minnesota-Dallas:
Info_Matchs(tournament='NHL', date=8.1, round=9, clock='15:00', score_home=[0, 1, 1, 5])
and other matchs...

The problem occurs when I uncheck and then check the checkbox again, because the values in the list are duplicated with each click on the checkbox with append(). On each click, append() adds a new list by adding it to the previous list and i get score_home=[0, 1, 1, 5, 0, 1, 1, 5]. I would like to remove the list and add it to each click, or add it and update it, and always get only score_home=[0, 1, 1, 5].

The problem should be this piece of code (below is also the full code):

for row in previous_results_query.fetchall():
    if row[0] not in test:
        info = Info_Match(
                tournament=row[1],
                date=row[2],
                rounds=row[3],
                clock=row[4],
                score_home=list())
    
    
    #Append Score_Home
        test[row[0]] = info
    test[row[0]].score_home.append(row[5])
            
print(test) 

Note: row[0] is the name of the match (e.g. Minnesota-Dallas), while row[5] is the list of score_home results (e.g. 0, 1, 1, 5)


DETAILED EXPLANATION OF THE PROBLEM WITH OUTPUT

If i click on the checkbox, the function starts and i print this (score_home=[0, 1, 1, 5]):

Minnesota-Dallas:
Info_Matchs(tournament='NHL', date=8.1, round=9, clock='15:00', score_home=[0, 1, 1, 5], score_away=[0, 0, 0, 3])
and other matchs...

Later, if I uncheck the checkbox, the function starts again So I get this result. Look at score_home (score_home=[0, 1, 1, 5, 0, 1, 1, 5]), another 0, 1, 1, 5 has been added:

Minnesota-Dallas:
Info_Matchs(tournament='NHL', date=8.1, round=9, clock='15:00', score_home=[0, 1, 1, 5, 0, 1, 1, 5], score_away=[0, 1, 1, 5])
and other matchs...

If I click on the checkbox and select it, i get this result. Look at score_home again (score_home=[0, 1, 1, 5, 0, 1, 1, 5, 0, 1, 1, 5,]), another 0, 1, 1, 5 has been added

Minnesota-Dallas:
Info_Matchs(tournament='NHL', date=8.1, round=9, clock='15:00', `score_home=[0, 1, 1, 5, 0, 1, 1, 5, 0, 1, 1, 5,]`, score_away=[0, 0, 0, 3])
and other matchs...

I want to get only output: score_home=[0, 1, 1, 5].


HOW DID I TRY TO FIX IT?

I tried to fix it with a try/except. score_home was no longer repeating and the problem seemed to be solved, but looking closer I got the output score_home=[0, 1, 5], instead I wanted to get score_home=[0, 1, 1, 5]. Tried like this, number 1 was removed maybe because it was a duplicate, but I didn't want that. I specify that without try/except, I print quietly score_home=[0, 1, 1, 5]

#Append Score_Home
    test[row[0]] = info

try:
    test[row[0]].score_home.remove(row[5])
except:
    pass
    
test[row[0]].score_home.append(row[5])
            
print(test) 

Code complete

import sqlite3
from tkinter import ttk
import tkinter as tk
from tkinter import *
from dataclasses import dataclass

root = tk.Tk()
root.geometry("200x200")

conn_test = sqlite3.connect('sql')
cursor_test = conn_test.cursor()

#dictionary
test = {}
next_match = {}

#list
datalist = []

@dataclass
class Info_Match:
    tournmant: str
    date: float
    rounds: int
    clock: str
    score_home: list[int]


#Next match
next_match_query = cursor_test.execute('''SELECT team_home||"-"||team_away, tournmant, date, round, clock FROM Next''')
for row in next_match_query.fetchall():
    if row[0] not in next_match:
        next_match[row[0]] = list(row[1:])


#Function of the checkbox
def func_previous_results():

    previous_results_query = 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;''')


    #Create dictionary with class
    for row in previous_results_query.fetchall():
        if row[0] not in test:
            info = Info_Match(
                    tournmant=row[1],
                    date=row[2],
                    rounds=row[3],
                    clock=row[4],
                    score_home=list())


        #Append Score_Home
            test[row[0]] = info
        test[row[0]].score_home.append(row[5])
        
    print(test)


#Checkbox
Checkbutton1 = tk.IntVar()
Button1 = tk.Checkbutton(root, text = "Checkbutton 1", variable = Checkbutton1, onvalue = 1, offvalue = 0, height = 1,
                         foreground='black', activebackground="#d9d9d9", highlightthickness = 0,
                         command= func_previous_results).place(x=10, y=30)

root.mainloop()

Output:

Minnesota-Dallas:
Info_Matchs(tournament='NHL', date=8.1, round=9, clock='15:00', score_home=[0, 1, 1, 5])
and other matchs...

Thank you



from Duplication of a list, with append, at each click on the checkbox. How can i avoid duplication?

No comments:

Post a Comment