Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1778377
  • 博文数量: 276
  • 博客积分: 1574
  • 博客等级: 上尉
  • 技术积分: 2894
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-26 23:23
个人简介

生活的美妙在于,不知道一下秒是惊艳还是伤神,时光流转,珍惜现在的拥有的时光

文章分类

全部博文(276)

文章存档

2017年(17)

2016年(131)

2015年(63)

2013年(2)

2012年(32)

2011年(31)

分类: Python/Ruby

2016-05-23 11:50:58


点击(此处)折叠或打开

  1. #!/usr/bin/env python3
  2. # -*- coding:utf-8 -*-
  3. def filetolist(file,listname):
  4.     try:
  5.         #打开文件
  6.         with open(file) as jaf:
  7.             #读取数据行
  8.             data = jaf.readline()
  9.         #转换成list
  10.         listname=data.strip().split(',')
  11.         return listname
  12.     except IOError as ioerr:
  13.         print('File error : %s' % ioerr)
  14.         return(None)
  15.         
  16. #处理字符,转换成m.s格式
  17. def sanitize(time_string):
  18.     if '-' in time_string:
  19.         splitter = '-'
  20.     elif ':' in time_string:
  21.         splitter = ':'
  22.     else:
  23.         return time_string
  24.     (min, sec) = time_string.split(splitter)
  25.     return (min + '.' + sec)

  26. for name in ["james", "julie", "mikey", "sarah"]:
  27.     thelist=filetolist(name+".txt",name)
  28.     cleanname = 'clean' + name
  29.     cleanname = []
  30.     for each_t in thelist:
  31.         cleanname.append(sanitize(each_t))
  32.     print(sorted(cleanname))
  33.     #使用列表推导式
  34.     print('use list comprehension')
  35.     cleanname2 = [sanitize(each_it) for each_it in thelist]
  36.     print(sorted(cleanname2))
  37.     #使用for 处理
  38. # sorted_name= sorted(cleanname2)
  39. # unique_name = 'unique_' + name
  40. # unique_name = []
  41. # for item in sorted_name:
  42. # if item not in unique_name:
  43. # unique_name.append(item)
  44. # print(unique_name[0:3])
  45.     #使用工厂函数set()
  46.     print(sorted(set(cleanname2))[0:3])
t@localhost 5$ python3 kelly.py
['2.01', '2.01', '2.22', '2.34', '2.34', '2.45', '3.01', '3.10', '3.21']
use list comprehension
['2.01', '2.01', '2.22', '2.34', '2.34', '2.45', '3.01', '3.10', '3.21']
['2.01', '2.22', '2.34']
['2.11', '2.11', '2.23', '2.23', '2.59', '3.10', '3.10', '3.21', '3.21']
use list comprehension
['2.11', '2.11', '2.23', '2.23', '2.59', '3.10', '3.10', '3.21', '3.21']
['2.11', '2.23', '2.59']
['2.22', '2.38', '2.49', '3.01', '3.01', '3.02', '3.02', '3.02', '3.22']
use list comprehension
['2.22', '2.38', '2.49', '3.01', '3.01', '3.02', '3.02', '3.02', '3.22']
['2.22', '2.38', '2.49']
['2.18', '2.25', '2.39', '2.54', '2.55', '2.55', '2.55', '2.58', '2.58']
use list comprehension
['2.18', '2.25', '2.39', '2.54', '2.55', '2.55', '2.55', '2.58', '2.58']
['2.18', '2.25', '2.39']
阅读(1284) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~