python 馬丁 quiz4

QUIZ 4

COMP9021 PRINCIPLES OF PROGRAMMING
$ python3 quiz_4.py

Enter an Indicator Name: Belly explosion by excessive Coca Cola consumption
Sorry, either the indicator of interest does not exist or it has no data.
$ python3 quiz_4.py

Enter an Indicator Name: Literacy rate, youth total (% of people ages 15-24)
The maximum value is: 100
It was reached in these years, for these countries or categories:
2007: ['Azerbaijan']
2013: ['Moldova']
$ python3 quiz_4.py

Enter an Indicator Name: Age population, age 12, female, interpolated
The maximum value is: 13193254
It was reached in these years, for these countries or categories:
2000: ['China']
$ python3 quiz_4.py

Enter an Indicator Name: Newborns protected against tetanus (%)
The maximum value is: 99
It was reached in these years, for these countries or categories:
2006: ['Bahamas, The']
2007: ['Bahamas, The']
2008: ['Bahamas, The', 'Bahrain']
2009: ['Bahamas, The']
2010: ['Bahamas, The']
2011: ['Bahamas, The']
2012: ['Bahamas, The']
2013: ['Bahamas, The', 'Guyana']
2014: ['Bahamas, The', 'Guyana']
$ python3 quiz_4.py

Enter an Indicator Name: Female headed households (% of households with a female head)
The maximum value is: 49.4
It was reached in these years, for these countries or categories:
2007: ['Ukraine']
$ python3 quiz_4.py

Enter an Indicator Name: Number of neonatal deaths
The maximum value is: 5106312
It was reached in these years, for these countries or categories:
1990: ['World']
$ python3 quiz_4.py

Enter an Indicator Name: Age at first marriage, female
The maximum value is: 33.7
It was reached in these years, for these countries or categories:
1991: ['St. Lucia']
Date: Trimester 1, 2019.
Quiz 4
Uses Heath Nutrition and Population statistics, stored in the file HNP_Data.csv.gz, assumed to be located in the working directory.

Prompts the user for an Indicator Name. If it exists and is associated with a numerical value for some countries or categories, for some the years 1960-2015, then finds out the maximum value, and outputs:

that value;

the years when that value was reached, from oldest to more recents years;

for each such year, the countries or categories for which that value was reached, listed in lexicographic order.

1 mark for the maximum value;

1.5 marks for the years and associated countries

import sys
import os
import csv
import gzip
#import pandas as pd

filename = 'HNP_Data.csv.gz'
if not os.path.exists(filename):
    print(f'There is no file named {filename} in the working directory, giving up...')
    sys.exit()

indicator_of_interest = input('Enter an Indicator Name: ')

first_year = 1960
number_of_years = 56
max_value = None
countries_for_max_value_per_year = {}

with gzip.open(filename) as csvfile:
    file = csv.reader(line.decode('utf8').replace('\0', '') for line in csvfile)
    
    my_columns = [0]
    my_table=[]
    x=0
    for i in range(4,60):
        my_columns.append(i)
    for line in file:
        my_table.append([])
        if indicator_of_interest in line:
            for index_value in my_columns:
                my_table[x].append(line[index_value])
            x=x+1
    if len(my_table)!=[]:
        max_v=0.0
        LL=[]
        c_L=[]
        x,y=0,0
        for i in range(len(my_table)):
            for j in range(1,len(my_table[i])):
                if my_table[i][j]!='' and float(my_table[i][j])>max_v:
                    max_v=float(my_table[i][j])
                    x,y=i,j
                    LL=[]
                    c=my_table[i][0]
                    LL.append((j+1959,c))
                elif my_table[i][j]!='' and float(my_table[i][j])==max_v:
                    c=my_table[i][0]
                    LL.append((j+1959,c))
        index=0
        index_tem=1
        if max_v>0:
            max_value=my_table[x][y]
            LL.sort()
            countries_for_max_value_per_year.update({LL[0][0]:[LL[0][1]]})
            for i in range(1,len(LL)):
                if LL[i-1][0]==LL[i][0]:
                    countries_for_max_value_per_year[LL[index][0]].append(LL[i][1])
                    index_tem=index_tem+1
                else:
                    index=index+index_tem
                    index_tem=1
                    countries_for_max_value_per_year.update({LL[index][0]:[LL[i][1]]})
    
    
if max_value is None:
    print('Sorry, either the indicator of interest does not exist or it has no data.')
else:
    print('The maximum value is:', max_value)
    print('It was reached in these years, for these countries or categories:')
    print('\n'.join(f'    {year}: {countries_for_max_value_per_year[year]}'
                                  for year in sorted(countries_for_max_value_per_year)
                   )
         )
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末枉昏,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡灵巧,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進店門抹沪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來刻肄,“玉大人,你說我怎么就攤上這事融欧∶羝” “怎么了?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵噪馏,是天一觀的道長麦到。 經(jīng)常有香客問我绿饵,道長,這世上最難降的妖魔是什么瓶颠? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任拟赊,我火速辦了婚禮,結(jié)果婚禮上粹淋,老公的妹妹穿的比我還像新娘吸祟。我一直安慰自己,他們只是感情好桃移,可當我...
    茶點故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布欢搜。 她就那樣靜靜地躺著,像睡著了一般谴轮。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上吹埠,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天第步,我揣著相機與錄音,去河邊找鬼缘琅。 笑死粘都,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的刷袍。 我是一名探鬼主播翩隧,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼呻纹!你這毒婦竟也來了堆生?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤雷酪,失蹤者是張志新(化名)和其女友劉穎淑仆,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體哥力,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡蔗怠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了吩跋。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片寞射。...
    茶點故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖锌钮,靈堂內(nèi)的尸體忽然破棺而出桥温,到底是詐尸還是另有隱情,我是刑警寧澤梁丘,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布策治,位于F島的核電站脓魏,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏通惫。R本人自食惡果不足惜茂翔,卻給世界環(huán)境...
    茶點故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望履腋。 院中可真熱鬧珊燎,春花似錦、人聲如沸遵湖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽延旧。三九已至谋国,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間迁沫,已是汗流浹背芦瘾。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留集畅,地道東北人近弟。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像挺智,于是被迫代替她去往敵國和親祷愉。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,914評論 2 355

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