数据来源:https://github.com/fivethirtyeight/data/tree/master/nfl-suspensions 美国国家橄榄球队停赛记录。
要求:
1.提取队伍名字、比赛名字
2.创建队伍类
重点:set使用、类的创建
import csv
nfl_suspensions=list(csv.reader(open("nfl_suspensions_data.csv","r")))
nfl_suspensions=nfl_suspensions[1:] #去掉header
teams=[row[1] for row in nfl_suspensions]
games=[row[2] for row in nfl_suspensions]
unique_teams=set(teams)
unique_games=set(games)
class Suspension():
def __init__(self,row):
self.name = row[0]
self.team = row[1]
self.games = row[2]
try:
self.year=int(row[5]) #Convert year column to integer type.
except Exception:
self.year=0
def get_year(self):
return self.year
third_suspension=Suspension(nfl_suspensions[2])
third_year=third_suspension.get_year()
网友评论