計算機科学のブログ

ほしい物リスト

Python - List of Files: Functions, Modules and Files - if statement, in

Head First Python: A Learner’s Guide to the Fundamentals of Python Programming, A Brain-Friendly GuidePaul Barry(著)、 O’Reilly Mediaの Chapter 4.(List of Files: Functions, Modules and Files)、SHARPEN YOUR PENCIL(233/682)の解答を求めてみる。

Jupyter(コード、入出力結果)

Times.ipynb

import os
import statistics

FOLDER = 'swimdata'
FN = 'Darius-13-100m-Fly.txt'

swimmer, age, distance, stroke = FN.removesuffix('.txt').split('-')

with open(os.path.join(FOLDER, FN)) as f:
    lines = f.readlines()
times = lines[0].strip().split(',')
converts = []
for t in times:
    minutes, rest = t.split(':')
    seconds, hundredths = rest.split('.')
    converted_time = int(minutes) * 60 * 100 + int(seconds) * 100 + int(hundredths)
    converts.append(converted_time)
average = statistics.mean(converts)
mins_secs, hundredhs = str(round(average / 100, 2)).split('.')
mins_secs = int(mins_secs)
minutes, seconds = divmod(mins_secs, 60)
average = f'{minutes}:{seconds}.{hundredhs}'
swimmer, age, distance, stroke
('Darius', '13', '100m', 'Fly')
times
['1:27.95', '1:21.07', '1:30.96', '1:23.22', '1:27.95', '1:28.30']
average
'1:26.58'
import swimclub

data = swimclub.read_swim_data('Darius-13-100m-Fly.txt')
data
('Darius',
 '13',
 '100m',
 'Fly',
 ['1:27.95', '1:21.07', '1:30.96', '1:23.22', '1:27.95', '1:28.30'],
 '1:26.58')
times = data[4]
times
['1:27.95', '1:21.07', '1:30.96', '1:23.22', '1:27.95', '1:28.30']
swimmer, age, distance, stroke, times2, average = data
swimmer, age, distance, stroke, times2, average
('Darius',
 '13',
 '100m',
 'Fly',
 ['1:27.95', '1:21.07', '1:30.96', '1:23.22', '1:27.95', '1:28.30'],
 '1:26.58')
swim_files = os.listdir(FOLDER)
swim_files
['Hannah-13-100m-Free.txt',
 'Darius-13-100m-Back.txt',
 'Owen-15-100m-Free.txt',
 'Mike-15-100m-Free.txt',
 'Hannah-13-100m-Back.txt',
 'Mike-15-100m-Back.txt',
 'Mike-15-100m-Fly.txt',
 'Abi-10-50m-Back.txt',
 'Ruth-13-200m-Free.txt',
 '.DS_Store',
 'Tasmin-15-100m-Back.txt',
 'Erika-15-100m-Free.txt',
 'Ruth-13-200m-Back.txt',
 'Abi-10-50m-Free.txt',
 'Maria-9-50m-Free.txt',
 'Elba-14-100m-Free.txt',
 'Tasmin-15-100m-Free.txt',
 'Abi-10-100m-Back.txt',
 'Abi-10-50m-Breast.txt',
 'Mike-15-200m-IM.txt',
 'Ali-12-100m-Back.txt',
 'Ruth-13-100m-Back.txt',
 'Chris-17-100m-Back.txt',
 'Ali-12-100m-Free.txt',
 'Darius-13-100m-Breast.txt',
 'Ruth-13-100m-Free.txt',
 'Aurora-13-50m-Free.txt',
 'Katie-9-100m-Breast.txt',
 'Alison-14-100m-Breast.txt',
 'Ruth-13-400m-Free.txt',
 'Emma-13-100m-Free.txt',
 'Calvin-9-50m-Fly.txt',
 'Darius-13-100m-Fly.txt',
 'Mike-15-200m-Free.txt',
 'Emma-13-100m-Breast.txt',
 'Tasmin-15-100m-Breast.txt',
 'Blake-15-100m-Free.txt',
 'Abi-10-100m-Breast.txt',
 'Chris-17-100m-Breast.txt',
 'Blake-15-100m-Back.txt',
 'Bill-18-200m-Back.txt',
 'Darius-13-200m-IM.txt',
 'Dave-17-100m-Free.txt',
 'Alison-14-100m-Free.txt',
 'Lizzie-14-100m-Free.txt',
 'Katie-9-50m-Fly.txt',
 'Katie-9-50m-Breast.txt',
 'Katie-9-50m-Back.txt',
 'Lizzie-14-100m-Back.txt',
 'Tasmin-15-200m-Breast.txt',
 'Katie-9-50m-Free.txt',
 'Dave-17-200m-Back.txt',
 'Erika-15-200m-Breast.txt',
 'Calvin-9-50m-Back.txt',
 'Calvin-9-50m-Free.txt',
 'Carl-15-100m-Back.txt',
 'Bill-18-100m-Back.txt',
 'Katie-9-100m-Free.txt',
 'Blake-15-100m-Fly.txt',
 'Erika-15-100m-Breast.txt',
 'Katie-9-100m-Back.txt']
sorted(swim_files)
['.DS_Store',
 'Abi-10-100m-Back.txt',
 'Abi-10-100m-Breast.txt',
 'Abi-10-50m-Back.txt',
 'Abi-10-50m-Breast.txt',
 'Abi-10-50m-Free.txt',
 'Ali-12-100m-Back.txt',
 'Ali-12-100m-Free.txt',
 'Alison-14-100m-Breast.txt',
 'Alison-14-100m-Free.txt',
 'Aurora-13-50m-Free.txt',
 'Bill-18-100m-Back.txt',
 'Bill-18-200m-Back.txt',
 'Blake-15-100m-Back.txt',
 'Blake-15-100m-Fly.txt',
 'Blake-15-100m-Free.txt',
 'Calvin-9-50m-Back.txt',
 'Calvin-9-50m-Fly.txt',
 'Calvin-9-50m-Free.txt',
 'Carl-15-100m-Back.txt',
 'Chris-17-100m-Back.txt',
 'Chris-17-100m-Breast.txt',
 'Darius-13-100m-Back.txt',
 'Darius-13-100m-Breast.txt',
 'Darius-13-100m-Fly.txt',
 'Darius-13-200m-IM.txt',
 'Dave-17-100m-Free.txt',
 'Dave-17-200m-Back.txt',
 'Elba-14-100m-Free.txt',
 'Emma-13-100m-Breast.txt',
 'Emma-13-100m-Free.txt',
 'Erika-15-100m-Breast.txt',
 'Erika-15-100m-Free.txt',
 'Erika-15-200m-Breast.txt',
 'Hannah-13-100m-Back.txt',
 'Hannah-13-100m-Free.txt',
 'Katie-9-100m-Back.txt',
 'Katie-9-100m-Breast.txt',
 'Katie-9-100m-Free.txt',
 'Katie-9-50m-Back.txt',
 'Katie-9-50m-Breast.txt',
 'Katie-9-50m-Fly.txt',
 'Katie-9-50m-Free.txt',
 'Lizzie-14-100m-Back.txt',
 'Lizzie-14-100m-Free.txt',
 'Maria-9-50m-Free.txt',
 'Mike-15-100m-Back.txt',
 'Mike-15-100m-Fly.txt',
 'Mike-15-100m-Free.txt',
 'Mike-15-200m-Free.txt',
 'Mike-15-200m-IM.txt',
 'Owen-15-100m-Free.txt',
 'Ruth-13-100m-Back.txt',
 'Ruth-13-100m-Free.txt',
 'Ruth-13-200m-Back.txt',
 'Ruth-13-200m-Free.txt',
 'Ruth-13-400m-Free.txt',
 'Tasmin-15-100m-Back.txt',
 'Tasmin-15-100m-Breast.txt',
 'Tasmin-15-100m-Free.txt',
 'Tasmin-15-200m-Breast.txt']
swim_files.remove('.DS_Store')
sorted(swim_files)
['Abi-10-100m-Back.txt',
 'Abi-10-100m-Breast.txt',
 'Abi-10-50m-Back.txt',
 'Abi-10-50m-Breast.txt',
 'Abi-10-50m-Free.txt',
 'Ali-12-100m-Back.txt',
 'Ali-12-100m-Free.txt',
 'Alison-14-100m-Breast.txt',
 'Alison-14-100m-Free.txt',
 'Aurora-13-50m-Free.txt',
 'Bill-18-100m-Back.txt',
 'Bill-18-200m-Back.txt',
 'Blake-15-100m-Back.txt',
 'Blake-15-100m-Fly.txt',
 'Blake-15-100m-Free.txt',
 'Calvin-9-50m-Back.txt',
 'Calvin-9-50m-Fly.txt',
 'Calvin-9-50m-Free.txt',
 'Carl-15-100m-Back.txt',
 'Chris-17-100m-Back.txt',
 'Chris-17-100m-Breast.txt',
 'Darius-13-100m-Back.txt',
 'Darius-13-100m-Breast.txt',
 'Darius-13-100m-Fly.txt',
 'Darius-13-200m-IM.txt',
 'Dave-17-100m-Free.txt',
 'Dave-17-200m-Back.txt',
 'Elba-14-100m-Free.txt',
 'Emma-13-100m-Breast.txt',
 'Emma-13-100m-Free.txt',
 'Erika-15-100m-Breast.txt',
 'Erika-15-100m-Free.txt',
 'Erika-15-200m-Breast.txt',
 'Hannah-13-100m-Back.txt',
 'Hannah-13-100m-Free.txt',
 'Katie-9-100m-Back.txt',
 'Katie-9-100m-Breast.txt',
 'Katie-9-100m-Free.txt',
 'Katie-9-50m-Back.txt',
 'Katie-9-50m-Breast.txt',
 'Katie-9-50m-Fly.txt',
 'Katie-9-50m-Free.txt',
 'Lizzie-14-100m-Back.txt',
 'Lizzie-14-100m-Free.txt',
 'Maria-9-50m-Free.txt',
 'Mike-15-100m-Back.txt',
 'Mike-15-100m-Fly.txt',
 'Mike-15-100m-Free.txt',
 'Mike-15-200m-Free.txt',
 'Mike-15-200m-IM.txt',
 'Owen-15-100m-Free.txt',
 'Ruth-13-100m-Back.txt',
 'Ruth-13-100m-Free.txt',
 'Ruth-13-200m-Back.txt',
 'Ruth-13-200m-Free.txt',
 'Ruth-13-400m-Free.txt',
 'Tasmin-15-100m-Back.txt',
 'Tasmin-15-100m-Breast.txt',
 'Tasmin-15-100m-Free.txt',
 'Tasmin-15-200m-Breast.txt']
for filename in swim_files:
    print(filename)
    swimclub.read_swim_data(filename)
Hannah-13-100m-Free.txt
Darius-13-100m-Back.txt
Owen-15-100m-Free.txt
Mike-15-100m-Free.txt
Hannah-13-100m-Back.txt
Mike-15-100m-Back.txt
Mike-15-100m-Fly.txt


Abi-10-50m-Back.txt
Ruth-13-200m-Free.txt
Tasmin-15-100m-Back.txt
Erika-15-100m-Free.txt
Ruth-13-200m-Back.txt
Abi-10-50m-Free.txt
Maria-9-50m-Free.txt
Elba-14-100m-Free.txt
Tasmin-15-100m-Free.txt
Abi-10-100m-Back.txt
Abi-10-50m-Breast.txt
Mike-15-200m-IM.txt
Ali-12-100m-Back.txt
Ruth-13-100m-Back.txt
Chris-17-100m-Back.txt
Ali-12-100m-Free.txt
Darius-13-100m-Breast.txt
Ruth-13-100m-Free.txt
Aurora-13-50m-Free.txt
Katie-9-100m-Breast.txt
Alison-14-100m-Breast.txt
Ruth-13-400m-Free.txt
Emma-13-100m-Free.txt
Calvin-9-50m-Fly.txt
Darius-13-100m-Fly.txt
Mike-15-200m-Free.txt
Emma-13-100m-Breast.txt
Tasmin-15-100m-Breast.txt
Blake-15-100m-Free.txt
Abi-10-100m-Breast.txt
Chris-17-100m-Breast.txt
Blake-15-100m-Back.txt
Bill-18-200m-Back.txt
Darius-13-200m-IM.txt
Dave-17-100m-Free.txt
Alison-14-100m-Free.txt
Lizzie-14-100m-Free.txt
Katie-9-50m-Fly.txt
Katie-9-50m-Breast.txt
Katie-9-50m-Back.txt
Lizzie-14-100m-Back.txt
Tasmin-15-200m-Breast.txt
Katie-9-50m-Free.txt
Dave-17-200m-Back.txt
Erika-15-200m-Breast.txt
Calvin-9-50m-Back.txt
Calvin-9-50m-Free.txt
Carl-15-100m-Back.txt
Bill-18-100m-Back.txt
Katie-9-100m-Free.txt
Blake-15-100m-Fly.txt
Erika-15-100m-Breast.txt
Katie-9-100m-Back.txt

コード

swimclub.py

import os
import statistics

FOLDER = 'swimdata'


def read_swim_data(filename: str):
    swimmer, age, distance, stroke = filename.removesuffix('.txt').split('-')
    with open(os.path.join(FOLDER, filename)) as f:
        lines = f.readlines()
        times = lines[0].strip().split(',')
    converts = []
    for t in times:
        if ':' in t:
            minutes, rest = t.split(':')
        else:
            minutes = 0
            rest = t
        seconds, hundredths = rest.split('.')
        converts.append(
            int(minutes) * 60 * 100 + int(seconds) * 100 + int(hundredths)
        )
        average = statistics.mean(converts)
        mins_secs, hundredths = str(round(average / 100, 2)).split('.')
        mins_secs = int(mins_secs)
        minutes, seconds = divmod(mins_secs, 60)
        average = f'{minutes}:{seconds}.{hundredths}'
    return swimmer, age, distance, stroke, times, average