2014年6月14日土曜日

開発環境

Head First Python (Paul Barry(著)、 O'Reilly Media )のChapter 5(Comprehending data: Work that data)、DO THIS!(p.167)を解いてみる。

DO THIS!(p.167)

コード(BBEdit)

sample167.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(',')

for times in [set(sanitize(t) for t in times)
              for times in [james, sarah, julie, mikey]]:
    print(sorted(times)[:3])

入出力結果(Terminal)

$ ./sample167.py
['2.01', '2.22', '2.34']
['2.18', '2.25', '2.39']
['2.11', '2.23', '2.59']
['2.22', '2.38', '2.49']
$

0 コメント:

コメントを投稿