Philip Shangguan
Philip Shangguan

Reputation: 527

problems with dictionary issue in python code

My Data Science professor posted some code that we are supposed to follow for homework. Here are part of it:

def create_compare_df() -> pd.DataFrame: 
 """Generate comparison dataframe for lists. 
   Returns 
 -------- 
 pd.Dataframe 
 Pandas data frame containing time metrics for selection sort algorithm  -------- 
 """ 
  
 compare: dict = { 
 "array_length" : [512, 1024, 2048, 4096, 8192], 
 "sorted_time": [], 
 "binarysearch_time": [], 
 "linearsearch_time": [], 
 } 
  
 for i in compare["array_length"]: 
.........

I do not know what is the "Compare: dict = ..." part, and wehn I tested the code, it says "compare" is not defined...

I have never seen dictionary defined as above. any idea?

Thanks, Philip

Upvotes: 0

Views: 124

Answers (2)

Akshay Saambram
Akshay Saambram

Reputation: 350

compare is a variable name given to it! Your code may work if you go as follows...

compare = { 
    "array_length" : [512, 1024, 2048, 4096, 8192], 
    "sorted_time": [], 
    "binarysearch_time": [], 
    "linearsearch_time": [] 
}

Upvotes: 1

Bag
Bag

Reputation: 75

I would remove the dict = {} part of the equation as it could be used for refrencing. What I would do is the following:

 def create_compare_df() -> pd.DataFrame: 
     """Generate comparison dataframe for lists. 
       Returns 
     -------- 
     pd.Dataframe 
     Pandas data frame containing time metrics for selection sort algorithm  -------- 
 """ 
  
     compare = { 
     "array_length" : [512, 1024, 2048, 4096, 8192], 
     "sorted_time": [], 
    "binarysearch_time": [], 
    "linearsearch_time": [], 
    } 
  
    print(compare)

Basically, you have to be careful with your indentation. So make sure there is a tab in the lines after def.

Let me know if the problem still pertains.

Upvotes: 1

Related Questions