Python - Flask - gazpacho - Working with Data - Data Manipulationg - string, methods, removesuffix, splet, dictionary
Head First Python: A Learner’s Guide to the Fundamentals of Python Programming, A Brain-Friendly Guide、 Paul Barry(著)、 O’Reilly Mediaの Chapter 10.(Working with Data: Data Manipulation)、EXERCISE(469/682)の解答を求めてみる。
Jupyter(コード、入出力結果)
WorldRecords.ipynb
import swimclub
swimclub.event_lookup('Darius-13-100m-IM.txt') == '100m individual medley'
True
コード
def event_lookup(filename: str):
conversions = {
'Free': 'freestyle',
'Back': 'backstroke',
'Breast': 'breaststroke',
'Fly': 'butterfly',
'IM': 'individual medley',
}
*_, distance, stroke = filename.removesuffix('.txt').split('-')
return f'{distance} {conversions[stroke]}'