Python - List of Files: Functions, Modules and Files - in keyword, string
Head First Python: A Learner’s Guide to the Fundamentals of Python Programming, A Brain-Friendly Guide、 Paul Barry(著)、 O’Reilly Mediaの Chapter 4.(List of Files: Functions, Modules and Files)、SHARPEN YOUR PENCIL(83/108)の解答を求めてみる。
Jupyter(コード、入出力結果)
Files.ipynb
def read_swim_data(filename):
swimmer, age, distance, stroke = filename.removesuffix('.txt').split('-')
with open(FOLDER + filename) as file:
lines = file.readlines()
times = lines[0].rstrip('\n').split(',')
converts = []
for t in times:
if ':' in t:
minutes, rest = t.split(':')
seconds, hundredths = rest.split('.')
converted_time = (
int(minutes) * 60 * 100 + int(seconds) * 100 + int(hundredths)
)
else:
converted_time = 0
converts.append(converted_time)
# ここまで
average = statistics.mean(converts)
mins_secs, hundredths = str(round(average / 100, 2)).split('.')
mins_secs = int(mins_secs)
minutes = mins_secs // 60
seconds = mins_secs - minutes * 60
average = f'{minutes}:{seconds}.{hundredths}'
return (
swimmer,
age,
distance,
stroke,
times,
average,
)
for filename in swim_files:
print(filename)
print(read_swim_data(filename))
Hannah-13-100m-Free.txt
('Hannah', '13', '100m', 'Free', ['1:21.43', '1:21.40', '1:21.62', '1:25.38'], '1:22.46')
Darius-13-100m-Back.txt
('Darius', '13', '100m', 'Back', ['1:22.57', '1:29.64', '1:20.39', '1:23.83'], '1:24.11')
Owen-15-100m-Free.txt
('Owen', '15', '100m', 'Free', ['1:15.57', '1:14.40', '1:19.82', '1:12.90'], '1:15.67')
Mike-15-100m-Free.txt
('Mike', '15', '100m', 'Free', ['1:02.52', '1:02.56', '1:06.41', '1:07.08', '1:02.56', '1:03.23', '1:04.54'], '1:4.13')
Hannah-13-100m-Back.txt
('Hannah', '13', '100m', 'Back', ['1:35.75', '1:32.78', '1:34.01', '1:32.57'], '1:33.78')
Mike-15-100m-Back.txt
('Mike', '15', '100m', 'Back', ['1:19.23', '1:13.70', '1:13.96', '1:15.94'], '1:15.71')
Mike-15-100m-Fly.txt
('Mike', '15', '100m', 'Fly', ['1:14.55', '1:14.15', '1:08.38', '1:08.74', '1:10.89', '1:09.47', '1:09.52', '1:09.86', '1:09.83'], '1:10.6')
Abi-10-50m-Back.txt
('Abi', '10', '50m', 'Back', ['41.50', '43.58', '42.35', '43.35', '39.85', '40.53', '42.14', '39.18', '40.89', '40.89'], '0:0.0')
Ruth-13-200m-Free.txt
('Ruth', '13', '200m', 'Free', ['2:22.64', '2:28.16', '2:28.38', '2:25.08', '2:25.64', '2:33.58'], '2:27.25')
Tasmin-15-100m-Back.txt
('Tasmin', '15', '100m', 'Back', ['1:19.63', '1:16.69', '1:19.68', '1:16.56'], '1:18.14')
Erika-15-100m-Free.txt
('Erika', '15', '100m', 'Free', ['1:19.04', '1:15.88', '1:18.62', '1:21.74'], '1:18.82')
Ruth-13-200m-Back.txt
('Ruth', '13', '200m', 'Back', ['2:56.68', '2:42.63', '2:40.90', '2:42.88', '2:46.09'], '2:45.84')
Abi-10-50m-Free.txt
('Abi', '10', '50m', 'Free', ['39.07', '37.66', '36.13', '39.42'], '0:0.0')
Maria-9-50m-Free.txt
('Maria', '9', '50m', 'Free', ['45.75', '41.62', '40.49', '41.00'], '0:0.0')
Elba-14-100m-Free.txt
('Elba', '14', '100m', 'Free', ['1:06.01', '1:06.93', '1:05.34', '1:06.10'], '1:6.09')
Tasmin-15-100m-Free.txt
('Tasmin', '15', '100m', 'Free', ['1:08.87', '1:07.34', '1:11.00', '1:08.62', '1:10.94', '1:09.52', '1:12.04', '1:10.91'], '1:9.91')
Abi-10-100m-Back.txt
('Abi', '10', '100m', 'Back', ['1:31.59', '1:26.55', '1:28.75', '1:39.79', '1:32.37'], '1:31.81')
Abi-10-50m-Breast.txt
('Abi', '10', '50m', 'Breast', ['48.08', '53.17', '45.76', '49.28', '52.85', '46.19', '47.76', '48.83', '45.71', '47.76'], '0:0.0')
Mike-15-200m-IM.txt
('Mike', '15', '200m', 'IM', ['2:48.05', '2:44.03', '2:38.93', '2:54.86'], '2:46.47')
Ali-12-100m-Back.txt
('Ali', '12', '100m', 'Back', ['1:36.54', '1:39.10', '1:39.45', '1:42.87'], '1:39.49')
Ruth-13-100m-Back.txt
('Ruth', '13', '100m', 'Back', ['1:17.16', '1:16.65', '1:23.11', '1:14.37', '1:18.16', '1:18.28', '1:16.87', '1:17.37', '1:20.46'], '1:18.05')
Chris-17-100m-Back.txt
('Chris', '17', '100m', 'Back', ['1:21.76', '1:23.32', '1:24.09', '1:21.51'], '1:22.67')
Ali-12-100m-Free.txt
('Ali', '12', '100m', 'Free', ['1:27.31', '1:27.00', '1:27.46', '1:22.50', '1:20.39', '1:26.04', '1:20.22'], '1:24.42')
Darius-13-100m-Breast.txt
('Darius', '13', '100m', 'Breast', ['1:30.24', '1:33.18', '1:32.73', '1:33.05', '1:30.81', '1:31.74', '1:31.53', '1:33.05'], '1:32.04')
Ruth-13-100m-Free.txt
('Ruth', '13', '100m', 'Free', ['1:07.99', '1:11.87', '1:11.08', '1:09.75'], '1:10.17')
Aurora-13-50m-Free.txt
('Aurora', '13', '50m', 'Free', ['30.05', '30.21', '28.86', '30.65', '30.03', '29.64', '29.82'], '0:0.0')
Katie-9-100m-Breast.txt
('Katie', '9', '100m', 'Breast', ['1:50.10', '1:47.12', '1:54.65', '1:51.03'], '1:50.72')
Alison-14-100m-Breast.txt
('Alison', '14', '100m', 'Breast', ['1:34.71', '1:37.87', '1:35.15', '1:37.20', '1:36.29', '1:36.00', '1:35.63', '1:35.42', '1:42.01'], '1:36.7')
Ruth-13-400m-Free.txt
('Ruth', '13', '400m', 'Free', ['5:11.32', '5:04.12', '5:20.67', '5:08.31', '5:04.32', '5:06.77', '5:13.01'], '5:9.79')
Emma-13-100m-Free.txt
('Emma', '13', '100m', 'Free', ['1:32.21', '1:33.68', '1:34.10', '1:28.57'], '1:32.14')
Calvin-9-50m-Fly.txt
('Calvin', '9', '50m', 'Fly', ['39.47', '37.30', '41.90', '42.85', '39.90', '38.66', '38.84', '37.65', '39.68', '37.18', '43.61'], '0:0.0')
Darius-13-100m-Fly.txt
('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')
Mike-15-200m-Free.txt
('Mike', '15', '200m', 'Free', ['2:19.77', '2:41.57', '2:27.88', '2:37.28'], '2:31.62')
Emma-13-100m-Breast.txt
('Emma', '13', '100m', 'Breast', ['2:01.15', '1:57.86', '1:59.38', '1:57.94', '1:56.97'], '1:58.66')
Tasmin-15-100m-Breast.txt
('Tasmin', '15', '100m', 'Breast', ['1:20.59', '1:26.48', '1:24.15', '1:21.96'], '1:23.3')
Blake-15-100m-Free.txt
('Blake', '15', '100m', 'Free', ['1:02.13', '1:03.25', '1:04.16', '1:03.17', '1:04.84', '1:03.88', '1:04.87'], '1:3.76')
Abi-10-100m-Breast.txt
('Abi', '10', '100m', 'Breast', ['1:42.97', '1:43.31', '1:43.50', '1:40.34'], '1:42.53')
Chris-17-100m-Breast.txt
('Chris', '17', '100m', 'Breast', ['1:28.86', '1:29.90', '1:28.12', '1:28.67'], '1:28.89')
Blake-15-100m-Back.txt
('Blake', '15', '100m', 'Back', ['1:12.71', '1:19.87', '1:14.94', '1:11.53', '1:14.94'], '1:14.8')
Bill-18-200m-Back.txt
('Bill', '18', '200m', 'Back', ['2:29.06', '2:21.49', '2:24.13', '2:29.40'], '2:26.02')
Darius-13-200m-IM.txt
('Darius', '13', '200m', 'IM', ['3:03.84', '3:04.27', '3:01.49', '3:04.27', '2:52.44'], '3:1.26')
Dave-17-100m-Free.txt
('Dave', '17', '100m', 'Free', ['59.21', '59.27', '58.67', '59.72', '59.17'], '0:0.0')
Alison-14-100m-Free.txt
('Alison', '14', '100m', 'Free', ['1:13.86', '1:12.00', '1:14.69', '1:10.01', '1:12.24', '1:11.00', '1:11.20', '1:12.28', '1:15.46', '1:16.03'], '1:12.88')
Lizzie-14-100m-Free.txt
('Lizzie', '14', '100m', 'Free', ['1:21.08', '1:11.23', '1:12.46', '1:16.73'], '1:15.38')
Katie-9-50m-Fly.txt
('Katie', '9', '50m', 'Fly', ['40.11', '45.43', '50.17', '43.58', '52.61'], '0:0.0')
Katie-9-50m-Breast.txt
('Katie', '9', '50m', 'Breast', ['47.44', '52.32', '57.16', '56.77'], '0:0.0')
Katie-9-50m-Back.txt
('Katie', '9', '50m', 'Back', ['43.25', '47.35', '46.12', '45.69'], '0:0.0')
Lizzie-14-100m-Back.txt
('Lizzie', '14', '100m', 'Back', ['1:27.10', '1:26.21', '1:26.42', '1:35.79', '1:30.47'], '1:29.2')
Tasmin-15-200m-Breast.txt
('Tasmin', '15', '200m', 'Breast', ['2:54.14', '3:03.58', '3:02.43', '2:57.99'], '2:59.53')
Katie-9-50m-Free.txt
('Katie', '9', '50m', 'Free', ['38.94', '37.76', '41.83', '42.81'], '0:0.0')
Dave-17-200m-Back.txt
('Dave', '17', '200m', 'Back', ['2:35.46', '2:28.30', '2:35.35', '2:28.84'], '2:31.99')
Erika-15-200m-Breast.txt
('Erika', '15', '200m', 'Breast', ['3:27.51', '3:34.70', '3:27.15', '3:28.45', '3:34.79', '3:30.21', '3:33.74'], '3:30.94')
Calvin-9-50m-Back.txt
('Calvin', '9', '50m', 'Back', ['38.79', '39.03', '41.29', '39.66', '41.13', '38.57', '39.40', '41.06', '40.37', '43.05', '37.85'], '0:0.0')
Calvin-9-50m-Free.txt
('Calvin', '9', '50m', 'Free', ['35.78', '36.19', '36.89', '34.29', '34.18', '35.94'], '0:0.0')
Carl-15-100m-Back.txt
('Carl', '15', '100m', 'Back', ['1:29.78', '1:28.38', '1:25.03', '1:25.66'], '1:27.21')
Bill-18-100m-Back.txt
('Bill', '18', '100m', 'Back', ['1:11.34', '1:05.81', '1:05.75', '1:05.82'], '1:7.18')
Katie-9-100m-Free.txt
('Katie', '9', '100m', 'Free', ['1:20.81', '1:23.61', '1:24.80', '1:22.05', '1:22.58', '1:23.94'], '1:22.97')
Blake-15-100m-Fly.txt
('Blake', '15', '100m', 'Fly', ['1:11.49', '1:10.16', '1:12.59', '1:11.63', '1:09.43'], '1:11.06')
Erika-15-100m-Breast.txt
('Erika', '15', '100m', 'Breast', ['1:36.72', '1:34.78', '1:34.05', '1:32.78', '1:33.51', '1:33.29', '1:36.47', '1:31.82'], '1:34.18')
Katie-9-100m-Back.txt
('Katie', '9', '100m', 'Back', ['1:33.36', '1:31.52', '1:28.10', '1:33.37', '1:32.89', '1:33.38', '1:32.03', '1:32.71'], '1:32.17')