Python Forum
Sorting Data Returned From Database
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting Data Returned From Database
#4
Hi

Firstly, sorry if it didn't sound like I had tried to solve the problem myself & this is for a personal learning project as a newbie.

noisefloor I did initially try something like that with 3 tables as you suggested, but I think with a large amount of data it was quite slow using a SQL query.

I do already have something similar working in a simpler way where only the recipe id's sorted by matches to ingredients in input string are returned see underneath. The second database table column is simpler for this with just the recipe ids for each ingredient:

UsersIngredientList= UsersIngredientList.split(",")
    recipeidlist=()
    counter=0
    while counter<len(UsersIngredientList):
        cursor.execute("select recipe_ids from ingredient where display_name = '"+UsersIngredientList[counter]+"'")        
        recipeidlist = sum((recipeidlist, cursor.fetchone()), ())
        counter+=1
    cursor.close()
    recipeidlist = ''.join(recipeidlist)   
    recipeidlist=recipeidlist.replace("</id>","")
    recipeidlist = list(recipeidlist.split("<id>")) 
    recipeidlist.pop(0)
    runtime = round(time.time() - start_time, 2)
    print ("Getting list of recipe ID's from database for users ingredients took ", runtime," seconds to run")

    start_time = time.time()  
    CleanedandSortedRecipeIDList = Counter(recipeidlist).most_common()
    runtime = round(time.time() - start_time, 2)
    print ("Sorting the recipe ID list by frequency then removing duplicates took ", runtime," seconds to run")

    start_time = time.time()
    if len(CleanedandSortedRecipeIDList)>500:
        CleanedandSortedRecipeIDList= CleanedandSortedRecipeIDList[0:500]     
    runtime = round(time.time() - start_time, 2)
    print ("Limiting recipe ID list to 500 items took ", runtime," seconds to run")

    start_time = time.time()
    counter = 0
    CleanedandSortedRecipeIDListString=""
    while counter<len(CleanedandSortedRecipeIDList):
        CleanedandSortedRecipeIDListString= CleanedandSortedRecipeIDListString+CleanedandSortedRecipeIDList[counter][0]+","
        counter = counter+1
    CleanedandSortedRecipeIDListString = CleanedandSortedRecipeIDListString[:-1]
    runtime = round(time.time() - start_time, 2)
    print ("Building a string from all the recipe ID's for the SQL statement took ", runtime," seconds to run")
Reply


Messages In This Thread
Sorting Data Returned From Database - by Timbo03 - Feb-11-2023, 03:33 PM
RE: Sorting Data Returned From Database - by Yoriz - Feb-11-2023, 03:50 PM
RE: Sorting Data Returned From Database - by Timbo03 - Feb-11-2023, 08:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  RE: Data isn't writing to the Database with Django and SQL Lite card51shor 15 5,397 Sep-16-2020, 08:27 AM
Last Post: metulburr
  Calling a Returned Value to Another Function valerydolce 9 6,788 Mar-28-2017, 09:54 PM
Last Post: valerydolce

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020