開発環境
- 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.151)を解いてみる。
EXERCISE(p.151)
コード(BBEdit)
sample151.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, open('sarah.txt') as f_sarah, \
open('julie.txt') as f_julie, open('mikey.txt') as f_mikey:
james = f_james.readline().strip().split(',')
sarah = f_sarah.readline().strip().split(',')
julie = f_julie.readline().strip().split(',')
mikey = f_mikey.readline().strip().split(',')
new_james = []
new_sarah = []
new_julie = []
new_mikey = []
for time_string in james:
new_james.append(sanitize(time_string))
for time_string in sarah:
new_sarah.append(sanitize(time_string))
for time_string in julie:
new_julie.append(sanitize(time_string))
for time_string in mikey:
new_mikey.append(sanitize(time_string))
print(sorted(new_james))
print(sorted(new_sarah))
print(sorted(new_julie))
print(sorted(new_mikey))
入出力結果(Terminal)
$ ./sample151.py ['2.01', '2.01', '2.22', '2.34', '2.34', '2.45', '3.01', '3.10', '3.21'] ['2.18', '2.25', '2.39', '2.54', '2.55', '2.55', '2.55', '2.58', '2.58'] ['2.11', '2.11', '2.23', '2.23', '2.59', '3.10', '3.10', '3.21', '3.21'] ['2.22', '2.38', '2.49', '3.01', '3.01', '3.02', '3.02', '3.02', '3.22'] $
0 コメント:
コメントを投稿