challenge :modules,error handling,list comprehensions
csv
module. nfl_suspensions_data.csv
. csv.reader()
and list()
methods to read the file into a list named nfl_suspensions
. nfl_suspensions
, which contains the header row of the CSV file. nfl_suspensions
, except the for the one at index 0. nfl_suspensions
. year
column occurs. for
loop to iterate over the list in nfl_suspensions
representing the year
column: year
column and assign it to row_year
. row_year
is already a key in years
, add 1 to the value for that key. row_year
isn't already a key in years
, set the value for the key to 1. 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.
team
column and assign the list to unique_teams
. team
column. set()
function to return a list containing only the unique values and assign to unique_teams
. games
column and assign the list to unique_games
. games
column. set()
function to return a list containing only the unique values and assign to unique_games
. 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)
Suspension
class. __init__()
method with the following criteria: Suspension
instance, we want to be able to pass in a list from nfl_suspensions
. name
, team
, games
and year
columns. Upon instantiation: name
property. team
property. games
property. year
property. 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. year
property directly, use a try except block that: year
property instead called get_year()
that returns the year value
for that Suspension
instance. Suspension
instance using the 23rd row, and assign it to missing_year
. 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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.