開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Python 3.6 (プログラミング言語)
Head First Python (Paul Barry (著)、O'Reilly Media)のChapter 13.(Advanced Iteration: Looping Like Crazy)の SHARPEN YOUR PENCIL(No. 9144) を取り組んでみる。
SHARPEN YOUR PENCIL(No. 9144)
コード(Emacs)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from datetime import datetime import pprint def convert2ampm(time24: str) -> str: return datetime.strptime(time24, '%H:%M').strftime('%I:%m%p') with open('buzzers.csv') as data: ignore = data.readline() flights = {} for line in data: k, v = line.strip().split(',') flights[k] = v pprint.pprint(flights) print() flights2 = {} for k, v in flights.items(): flights2[convert2ampm(k)] = v.title() pprint.pprint(flights2)
入出力結果(Terminal, IPython)
$ ./sample1.py {'09:35': 'FREEPORT', '09:55': 'WEST END', '10:45': 'TREASURE CAY', '11:45': 'ROCK SOUND', '12:00': 'TREASURE CAY', '17:00': 'FREEPORT', '17:55': 'ROCK SOUND', '19:00': 'WEST END'} {'05:01PM': 'Rock Sound', '07:01PM': 'West End', '09:01AM': 'West End', '10:01AM': 'Treasure Cay', '11:01AM': 'Rock Sound', '12:01PM': 'Treasure Cay'} $
0 コメント:
コメントを投稿