一. 实验目的
1. 掌握自定义类的创建和使用等操作
2. 掌握 matplotlib 模块的使用
二. 实验内容
文件UN.txt中存放193个联合国成员信息,每行包括一个国家的名称、所在大洲、人口(百万)和面积(平方英里),例如:
Canada,North America,34.8,3855000
France,Europe,66.3,211209
New Zealand,Australia/Oceania,4.4,103738
Nigeria,Africa,177.2,356669
Pakistan,Asia,196.2,310403
Peru,South America,30.1,496226
(a) 创建一个Nation类包括四个实例变量存储国家信息和一个名为pop_density方法计算一个国家的人口密度。用这个类编写一个程序包含193个词条的字典。每个词条形式如下:
name of a country: Nation object for that country
用文件UN.txt创建这个字典,将这个字典保存到一个名为nationsDict.dat的永久二进制文件中,同时将Nation类保存到nation.py文件中。
(b) 利用nationsDict.dat和nation.py文件编写一个程序(search.py),输入联合国成员国名字,显示这个国家所有信息。如:
Enter a country: Canada
Continent: North America
Population: 34,800,000
Area: 3,855,000.00 square miles
(c) 利用nationsDict.dat和nation.py文件编写一个程序(sort.py),输入一个大洲的名字,按照降序使用 matplotlib 的柱状图功能画出该大洲人口密度前10名的联合国成员国名字及对应的人口密度。
将文件 nationsDict.dat,nation.py,search.py,sort.py 打包上传,压缩文件命名为:学号_姓名_实验2
nation.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import pickle class nation: def __init__( self , name = ' ', continent=' ', pop=' ', area=' ', pop_density=' '): self ._name = name self ._continent = continent self ._pop = pop self ._area = area self ._pop_density = pop_density def setName( self , name): self ._name = name def setContinent( self , continent): self ._continent = continent def setPop( self , pop): self ._pop = pop def setArea( self , area): self ._area = area def getName( self , name): return self ._name def getContinent( self , continent): return self ._continent def getPop( self , pop): return self ._pop def getArea( self , area): return self ._area def pop_density( self ): return ( self ._pop / self ._area) def __str__( self ): return ( "The poplation density of" + str ( self ._name) + "is" + str ( self .pop_density())) f = open ( 'UN.txt' ) dict = {} for line in f: words = line.split( "," ) _nation = nation(continent = ' ', pop=' ', area=' ', pop_density=' ') _nation.setName(words[ 0 ]) _nation.setContinent(words[ 1 ]) _nation.setPop(words[ 2 ]) _nation.setArea(words[ 3 ]) dict [words[ 0 ]] = _nation outfile = open ( "nationsDict.dat" , 'wb' ) pickle.dump( dict ,outfile) outfile.close() |
search.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import pickle import nation def getDictionary(fileName): infile = open (fileName, 'rb' ) nations = pickle.load(infile) infile.close() return nations def inputNameOfNation(nations): nation = input ( "Input a name of a UN member nation: " ) while nation not in nations: print ( "Not a member of the UN.Please try again." ) nation = input ( "Input a name of a UN member nation: " ) def displayData(nations,nation): print ( "Continent:" , nations[nation][ 'continent' ]) print ( "Populaton:" ,nations[nation][ 'pop' ], "million people" ) print ( "Area:" ,nations[nation][ 'area' ], "square miles" ) nations = getDictionary( "nationsDict.dat" ) nation = inputNameOfNation(nations) displayData(nations,nation) |
sort.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import matplotlib.pyplot as plt import nation import pickle def getDictionary(fileName): infile = open (fileName, 'rb' ) nations = pickle.load(infile) infile.close() return nations nations = getDictionary( "nationsDict.dict" ) for i in nations[i]: nation.pop_density() nation.pop_density.sort plt.bar(data[ 'x' ], data[ 'y' ]) |
不建议任何人直接复制此代码
二月 | ||||||
---|---|---|---|---|---|---|
日 | 一 | 二 | 三 | 四 | 五 | 六 |
26 | 27 | 28 | 29 | 30 | 31 | 1 |
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 1 |