《Head First Programming》---python 4_文件和数组中的数据
2014-04-24 10:12
453 查看
本章主要是利用数组(python中是指代列表)和python内置函数sort实现对指定文本中的数据进行排序和显示。
文本result.txt中的数据如下所示:
Johnny 8.65
Juan 9.12
Joseph 8.45
Stacey 7.81
Aideen 8.05
Zack 7.21
Aaron 8.31
1.实现代码:
2.实现结果:
Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:37:12) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
The top scores were :
9.12
8.65
8.45
>>>
但是没有关联名称和数值...
3.利用hash(python里值字典)实现名称和数据的关联
hash实现代码如下:
scores = {} #hash空表
result_f = open("result.txt")
for line in result_f:
(name, score) = line.split()
scores[score] = name
result_f.close()
print("The top scores were : ")
for each_score in sorted(scores.keys(), reverse = True):
print('Surfer ' + scores[each_score] + ' scored ' + each_score)
4.实现结果:
Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:37:12) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
The top scores were :
Surfer Juan scored 9.12
Surfer Johnny scored 8.65
Surfer Joseph scored 8.45
Surfer Aaron scored 8.31
Surfer Aideen scored 8.05
Surfer Stacey scored 7.81
Surfer Zack scored 7.21
>>>
文本result.txt中的数据如下所示:
Johnny 8.65
Juan 9.12
Joseph 8.45
Stacey 7.81
Aideen 8.05
Zack 7.21
Aaron 8.31
1.实现代码:
scores = [] #空列表 result_f = open("result.txt") for line in result_f: (name, score) = line.split() scores.append(float(score)) result_f.close() scores.sort()#降序排序 scores.reverse()#反序排序 print("The top scores were : ") print(scores[0]) print(scores[1]) print(scores[2])
2.实现结果:
Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:37:12) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
The top scores were :
9.12
8.65
8.45
>>>
但是没有关联名称和数值...
3.利用hash(python里值字典)实现名称和数据的关联
hash实现代码如下:
scores = {} #hash空表
result_f = open("result.txt")
for line in result_f:
(name, score) = line.split()
scores[score] = name
result_f.close()
print("The top scores were : ")
for each_score in sorted(scores.keys(), reverse = True):
print('Surfer ' + scores[each_score] + ' scored ' + each_score)
4.实现结果:
Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:37:12) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
The top scores were :
Surfer Juan scored 9.12
Surfer Johnny scored 8.65
Surfer Joseph scored 8.45
Surfer Aaron scored 8.31
Surfer Aideen scored 8.05
Surfer Stacey scored 7.81
Surfer Zack scored 7.21
>>>
相关文章推荐
- Head First Python 学习笔记-Chapter4:持久化--将数据写入文件
- HeadFirstPython---------(二)第四章,持久存储,数据保存到文件
- Head First Python 第二章 函数模块&第三章 文件与异常&第四章 持久存储&第五章 处理数据
- 《Head First Programming》---python 2_文本数据
- python 读取.csv文件数据到数组(矩阵)
- [Head First Python]5. 推导数据:处理数据
- Head First Python 学习笔记-Chapter6:自定义数据对象:字典与类
- Head First Python之3文件与异常
- 《Head First Programming》---python 9_GUI Mixer
- Head First Python(推导数据)
- 《head first python》——理解数据:列表排序与集合
- python 读取.csv文件数据到数组(矩阵)的实例讲解
- HeadFirstPython---------(四)第六章(定制数据对象__打包代码与数据)
- Python初入门(三)(Head First Python 第三章 文件与异常)
- Head First Python ch_6 定制数据对象
- Python初入门(六)(Head First Python 第六章 打包代码与数据 ,字典,类与继承)
- 《Head First Programming》---python 1_开始编码
- 《head first python》——定制数据对象
- [Head First Python]3. 文件与异常:处理错误
- Head First Python---------(一)第一章(初识Python__人人都爱列表)、第二章(共享你的代码__函数模块)、第三章(文件与异常__处理错误)