challenge :modules,error handling,list comprehensions

5854 단어
In this challenge, you'll practice using modules, classes, and list comprehensions to process and represent a data set in Python. You'll be working with data on NFL player suspensions.
  • Read the dataset into a list of lists.
  • Import the csv module.
  • Create a File handler for nfl_suspensions_data.csv .
  • Use the csv.reader() and list() methods to read the file into a list named nfl_suspensions .

  • Remove the first list in nfl_suspensions , which contains the header row of the CSV file.
  • Select all lists in nfl_suspensions , except the for the one at index 0.
  • Assign the resulting list of lists back to the variable nfl_suspensions .

  • Count the number of times each value in the year column occurs.
  • Create an empty dictionary called years.
  • Use a for loop to iterate over the list in nfl_suspensions representing the year column:
  • Extract that row's value for the year column and assign it to row_year .
  • If row_year is already a key in years , add 1 to the value for that key.
  • If row_year isn't already a key in years , set the value for the key to 1.


  • Use the print() function to display the dictionary years .
  • import csv
    
    nfl_suspensions = list (csv.reader(open('nfl_suspensions_data.csv','r')))
    nfl_suspensions = nfl_suspensions[1:]
    
    years ={}
    for row in nfl_suspensions:
        year = row[5]
        if year in years:
            years[year]+=1
        else:
            years[year] =1
    print(years)
    

    Let's explore the values in these columns by using sets and list comprehensions.
  • Retrieve the unique values in the team column and assign the list to unique_teams .
  • Use a list comprehension to create a new list containined just the values in the team column.
  • Use the set() function to return a list containing only the unique values and assign to unique_teams .

  • Retrieve the unique values in the games column and assign the list to unique_games .
  • Use a list comprehension to create a new list containined just the values in the games column.
  • Use the set() function to return a list containing only the unique values and assign to unique_games .

  • Display unique_teams and unique_games .
  • teams = [row[1] for row in nfl_suspensions]
    unique_teams = set(teams)
    
    unique_games = set([row[2] for row in nfl_suspensions])
    
    print (unique_teams)
    print (unique_games)
    
  • Create the Suspension class.
  • Define the __init__() method with the following criteria:
  • The sole required parameter is a list representing a row from the dataset.
  • To create a Suspension instance, we want to be able to pass in a list from nfl_suspensions .

  • Currently, we're only interested in storing the name , team , games and year columns. Upon instantiation:
  • Set the name value for that row to the name property.
  • Set the team value for that row to the team property.
  • Set the games value for that row to the games property.
  • Set the year value for that row to the year property.



  • Create a Suspension instance using the third row in nfl_suspensions , and assign it to the variable third_suspension .
  • class Suspension:
        def __init__(self,row):
            self.name = row[0]
            self.team = row[1]
            self.games= row[2]
            self.year = row[3]
            
    third_suspension = Suspension(nfl_suspensions[2])
    
    print(third_suspension)
    print(third_suspension.team)
    print(third_suspension.name)
    
    

    Let's tweak the Suspension class a bit to extend its functionality. Right now, the value for year is a string, rather than an integer. Let's modify the Suspension class so that it stores the values as integers.
  • Instead of assigning the value at index 5 to the year property directly, use a try except block that:
  • Tries to cast the value at index 5 to an integer
  • If an exception is thrown, assign the value 0 to the year property instead

  • Create a method called get_year() that returns the year value for that Suspension instance.
  • Create a Suspension instance using the 23rd row, and assign it to missing_year .
  • Use the get_year() method to assign the year of the missing_year suspension instance to twenty_third_year .
  • class Suspension:
        def __init__(self,row):
            self.name = row[0]
            self.team = row[1]
            self.games= row[2]
            try :
                self.year =int(row[5])
            except Exception:
                self.year = 0
        def get_year(self):
            return self.year
        
    missing_year = Suspension(nfl_suspensions[22])
    twenty_third_year = missing_year.get_year()
    twenty_third_year
    

    좋은 웹페이지 즐겨찾기