点击(此处)折叠或打开
- #!/usr/bin/env python3
- # -*- coding:utf-8 -*-
- def filetolist(file,listname):
- try:
- #打开文件
- with open(file) as jaf:
- #读取数据行
- data = jaf.readline()
- #转换成list
- listname=data.strip().split(',')
- return listname
- except IOError as ioerr:
- print('File error : %s' % ioerr)
- return(None)
-
- #处理字符,转换成m.s格式
- def sanitize(time_string):
- if '-' in time_string:
- splitter = '-'
- elif ':' in time_string:
- splitter = ':'
- else:
- return time_string
- (min, sec) = time_string.split(splitter)
- return (min + '.' + sec)
-
- for name in ["james", "julie", "mikey", "sarah"]:
- thelist=filetolist(name+".txt",name)
- cleanname = 'clean' + name
- cleanname = []
- for each_t in thelist:
- cleanname.append(sanitize(each_t))
- print(sorted(cleanname))
- #使用列表推导式
- print('use list comprehension')
- cleanname2 = [sanitize(each_it) for each_it in thelist]
- print(sorted(cleanname2))
- #使用for 处理
- # sorted_name= sorted(cleanname2)
- # unique_name = 'unique_' + name
- # unique_name = []
- # for item in sorted_name:
- # if item not in unique_name:
- # unique_name.append(item)
- # print(unique_name[0:3])
- #使用工厂函数set()
- print(sorted(set(cleanname2))[0:3])
['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']