class python

If we wanted to extract a header, grab all the columns, and do a count of a dataset, we would have to have written all these functions in a module and imported them for every new dataset we used. This can get tedious over time because we use datasets all the time and we would want a consistent set of behaviors and attributes to use with them.

With classes, we bundle all of that data and behavior together in one location. An instance of the Dataset class is all we need to count unique terms in a dataset or get a file's header. Once we add behavior to a class, every instance of the class will be able to perform that behavior. As we develop our application, we can add more properties to classes to extend their functionality. Using classes and instances helps organize our code, and allows us to represent real-world concepts in well-defined code constructs.


image

Create a class called Dataset.

  • Inside the class, create a type attribute. Assign the value "csv" to it.

Create an instance of the Dataset class, and assign it to the variable dataset.

Print the type attribute of the dataset instance.

image

Add a data parameter to the __init__() method, and set the value to the self.data attribute.

Read the data from nfl.csv and set it to the variable nfl_data.

Make an instance of the class, passing in nfl_data to the __init__() method (when you call Dataset(...)).

  • Assign the result to the variable nfl_dataset.

Use the data attribute to access the underlying data for nfl_dataset and assign the result to the variable dataset_data.

image

Add an instance method print_data() that takes in a num_rowsargument.

  • This method should print out data up to the given amount of rows.

Create an instance of the Dataset class and initialize with the nfl_data. nfl_data is already loaded for you .

  • Assign it to the cariable nfl_dataset.

Call the print_data method , setting the num_rows parameter to 5.

image

Add the extract_header() code to the initializer and set the header data to self.header.

Create a variable called nfl_header and set it to the header attribute.

image

Add a method named column that takes in a label argument, finds the index of the header, and returns a list of the column data.

  • If the label is not in the header, you should return None.

Create a variable called year_column and set it to the return value of column('year').

Create a variable called player_column and set it to the return value of column('player').

image

Add a method to the Dataset class called count_unique() that takes in a label arguments.

Get the unique set of items from the column() method and return the total count.

Use the instance method to assign the number of unique term values of year to total_years.

image

Add a method to the Dataset class called __str__()

  • Convert the first 10 rows of self.data to a string and set it as the return value.

Create an instance of the class called nfl_dataset and call print on it.

image
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末碳却,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子笑旺,更是在濱河造成了極大的恐慌昼浦,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,807評(píng)論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件筒主,死亡現(xiàn)場(chǎng)離奇詭異关噪,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)乌妙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門使兔,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人冠胯,你說(shuō)我怎么就攤上這事火诸。” “怎么了荠察?”我有些...
    開封第一講書人閱讀 169,589評(píng)論 0 363
  • 文/不壞的土叔 我叫張陵置蜀,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我悉盆,道長(zhǎng)盯荤,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,188評(píng)論 1 300
  • 正文 為了忘掉前任焕盟,我火速辦了婚禮秋秤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘脚翘。我一直安慰自己灼卢,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,185評(píng)論 6 398
  • 文/花漫 我一把揭開白布来农。 她就那樣靜靜地躺著鞋真,像睡著了一般。 火紅的嫁衣襯著肌膚如雪沃于。 梳的紋絲不亂的頭發(fā)上涩咖,一...
    開封第一講書人閱讀 52,785評(píng)論 1 314
  • 那天,我揣著相機(jī)與錄音繁莹,去河邊找鬼檩互。 笑死,一個(gè)胖子當(dāng)著我的面吹牛咨演,可吹牛的內(nèi)容都是我干的闸昨。 我是一名探鬼主播,決...
    沈念sama閱讀 41,220評(píng)論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼饵较!你這毒婦竟也來(lái)了溉跃?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,167評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤告抄,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后嵌牺,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體打洼,經(jīng)...
    沈念sama閱讀 46,698評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,767評(píng)論 3 343
  • 正文 我和宋清朗相戀三年逆粹,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了募疮。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,912評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡僻弹,死狀恐怖阿浓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蹋绽,我是刑警寧澤芭毙,帶...
    沈念sama閱讀 36,572評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站卸耘,受9級(jí)特大地震影響退敦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蚣抗,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,254評(píng)論 3 336
  • 文/蒙蒙 一侈百、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧翰铡,春花似錦钝域、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至赂毯,卻和暖如春战虏,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背党涕。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工烦感, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人膛堤。 一個(gè)月前我還...
    沈念sama閱讀 49,359評(píng)論 3 379
  • 正文 我出身青樓手趣,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子绿渣,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,922評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,350評(píng)論 0 10
  • 離下班還有一個(gè)半小時(shí)朝群,我已經(jīng)按耐不住,為了不耽誤我今天拿到快遞中符,我已經(jīng)讓我媽去替我取了姜胖,我怕我下班到家的時(shí)候剛好快...
    會(huì)發(fā)光的黑暗少女閱讀 388評(píng)論 0 0
  • 一切的循環(huán)工作,都是為了多掙點(diǎn)錢 淀散,看著這個(gè)社會(huì)和我差不多的年輕人右莱,有時(shí)覺(jué)得自己也不算太差,但有時(shí)也覺(jué)得自己太無(wú)能...
    憶此閱讀 327評(píng)論 0 0
  • MediaEffects Sample (API23) OpenGL ES developer guide Ope...
    北疆_閱讀 879評(píng)論 0 0
  • 8月6號(hào)上午十一點(diǎn)到學(xué)校教室集中档插,老師在清點(diǎn)人數(shù)慢蜓。 準(zhǔn)備出發(fā)啦 到達(dá)軍訓(xùn)基地的孩子們 到達(dá)基地的第一頓飯。 晚飯后...
    大白鵝與小白兔閱讀 382評(píng)論 0 0