---
title: "python-Processing data"
author: "fancyboy2050"
date: "2012-11-12T16:58:29Z"
tags: ["编程"]
categories: ["编程语言"]
slug: "python-processing-data"
draft: false
lastmod: "2012-11-12T16:58:29Z"
id: 1723126
url: "https://fancyboy2050.iteye.com/blog/1723126"
---

```java

def sanitize(time_string):
    if '-' in time_string:
        splitter = "-"
    elif ":" in time_string:
        splitter = ":"
    else:
        return(time_string)
    (mins, secs) = time_string.split(splitter)
    return (mins+"."+secs)

def get_coach_data(filename):
    try:
        with open(filename) as f:
            data = f.readline()
        list_data = data.strip().split(',')
        sarah_data = {"name":list_data.pop(0),
                      "dob":list_data.pop(0),
                      "times":sorted(set([sanitize(t) for t in list_data]))[0:3]}
        return sarah_data
    except IOError as ioerr:
        print("file error: "+str(ioerr))
        return(None)

sarah = get_coach_data("sarah.txt")
print(sarah["name"]+"'s fastest times are: "+str(sarah["times"]))

```



```java

sarah.txt：
Sarah Sweeney,2002-6-9,2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55,2:22,2-21,2.22

```


string的strip()函数，去除空格,split()分割字符返回列表
set()无序的不重复的集合
sorted()排序算法，返回一个原序列的副本，而原来的序列不发生变化
sort()排序算法，直接改变原来的序列的顺序返回
sort与sorted默认都是升序，有一个reverse的参数，传入true时为逆序
程序中sarah_data为字典数据结构
字典结构定义方式：
sarah_data = {}
sarah_data = dict()
程序中：sorted(set())一段函数式编程