0

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

5
  • This is a type annotation used poorly. Your code should work if you remove : dict. Commented Jul 1, 2021 at 16:44
  • I tried that and just did a "print(compare)" and got the error saying "NameError: name 'compare' is not defined". Commented Jul 1, 2021 at 16:49
  • You must have typed something wrong. If you wrote compare = { ... } that should define the variable. Commented Jul 1, 2021 at 16:53
  • Is your print(compare) inside the function? The variable is local, so you can't print it outside the function. Commented Jul 1, 2021 at 16:53
  • Make sure you copied the indentation correctly. This is why we recommend 4 spaces of indentation, so it's easily visible. Commented Jul 1, 2021 at 16:54

2 Answers 2

1

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": [] 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks it works. I got another problem with the code and that will be another question.
: dict is nothing like type casting. :-)
1

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.

2 Comments

Thanks it works. I got another problem with the code and that will be another question.
No problem. Happy to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.