pingh14

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

2013年11月23日

摘要: head first python中的一个数据处理的例子有四个U10选手的600米成绩,请取出每个选手跑的最快的3个时间。以下是四位选手的9次成绩James2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22Julie2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21Mikey2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38Sarah2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55代码如下:def sanitize(time_string): 阅读全文
posted @ 2013-11-23 23:22 pingh14 阅读(343) 评论(0) 推荐(0) 编辑

摘要: 以写模式打开文件:需要指定写模式,如下所示data = open('data.out','w')如果文件已经存在,则会清空它现有的所有内容。要追加一个文件,需要使用访问模式a,会追加到下一行。例子:将上节中Man和Other Man说的话,分别保存到两个文件中man = []other = []try: data = open('sketch.txt') for each_line in data: try: (role, line_spoken) = each_line.split(':') line_spoken = l... 阅读全文
posted @ 2013-11-23 17:57 pingh14 阅读(6051) 评论(0) 推荐(0) 编辑

摘要: Python中使用open BIF与文件交互,与for语句结合使用,一次读取一行读取文件sketch.txt,文件内容如下:Man: Ah! (taking out his wallet and paying) Just the five minutes.Other Man: Just the five minutes. Thank you.Other Man: Now let's get one thing quite clear: I most definitely told you!Man: Oh no you didn't!Other Man: Oh yes I did 阅读全文
posted @ 2013-11-23 16:40 pingh14 阅读(394) 评论(0) 推荐(0) 编辑

摘要: python中有两种列表,分别用()和[]表示:例如:letter = ('a','b','c')letter = ['a','b','c']用小括号表示的列表初始化后不允许修改,而中中括号生成的列表可以修改。例子:列表中的列表movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91, ["Graham Chapman", ["Michael P 阅读全文
posted @ 2013-11-23 13:39 pingh14 阅读(235) 评论(0) 推荐(0) 编辑