-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKT3 Python example.py
More file actions
59 lines (35 loc) · 1.37 KB
/
Copy pathKT3 Python example.py
File metadata and controls
59 lines (35 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import csv
def get_books(filename):
with open(filename, 'r', encoding='cp1251') as file:
reader = csv.reader(file, delimiter='|')
rows = list(filter(lambda row: row and any(row), reader))
return rows
def filtered_books(books, substring):
header = books[0]
title_index = header.index('title')
data_rows = books[1:]
filtered_data = filter(
lambda row: len(row) > title_index and substring.lower() in row[title_index].lower(),
data_rows
)
return [header] + list(filtered_data)
def transform_books(books):
header = books[0]
isbn_index = header.index('isbn')
quantity_index = header.index('quantity')
price_index = header.index('price')
data_rows = books[1:]
transformed = map(
lambda row: (
row[isbn_index],
float(row[quantity_index]) * float(row[price_index])
),
data_rows
)
return list(transformed)
books = get_books("books.csv")
print("Задание 1 (первые 2 строки):", books[:2])
python_books = filtered_books(books, "python")
print("\nЗадание 2 (отфильтровано):", python_books)
result = transform_books(python_books)
print("\nЗадание 3 (результат):", result)