I have a huge list of dictionary with key-value pairs. There are close to 5000 key-value pairs. Also, there are close to 50 if condition statements in my program. The program runs continuously and during each run, it goes through 50 if statements which is impacting performance.
Below is a sample snippet just for reference.
Is there a better way to check multiple conditions and improve performance?
import mysql.connector
mySQLconnection = mysql.connector.connect(host='localhost', user='root', password='root')
cursor = mySQLconnection.cursor()
test_list = [{'A':1,'B':2,'C':3,'D':4}]
for i in test_list:
if(i['A'] == 1):
print(i['A'])
sql_select_Query = "insert into a values(%s)"
cursor.execute(sql_select_Query,(i['A']))
mySQLconnection.commit()
if(i['B'] == 2):
print(i['A'])
sql_select_Query = "insert into a values(%s)"
cursor.execute(sql_select_Query,(i['B']))
mySQLconnection.commit()
if(i['C'] == 3):
print(i['A'])
sql_select_Query = "insert into a values(%s)"
cursor.execute(sql_select_Query,(i['C']))
mySQLconnection.commit()
Aucun commentaire:
Enregistrer un commentaire