開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Head First Python (Paul Barry(著)、 O'Reilly Media )のChapter 5(Comprehending data: Work that data)、EXERCISE(p.161)を解いてみる。
EXERCISE(p.161)
コード(BBEdit)
sample161.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- def sanitize(time_string): if '-' in time_string: spliter = '-' elif ':' in time_string: spliter = ':' else: return (time_string) (mins, secs) = time_string.split(spliter) return (mins + '.' + secs) with open('james.txt') as f: james = f.readline().strip().split(',') james = sorted([sanitize(t) for t in james]) unique_james = [] for time_string in james: if not time_string in unique_james: unique_james.append(time_string) print(unique_james[:3])
入出力結果(Terminal)
$ ./sample161.py ['2.01', '2.22', '2.34'] $
0 コメント:
コメントを投稿