Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # решение здесь
- #выгрузить данные из файла
- raw_list =[]
- with open("problem5.csv",encoding="utf8") as fh:
- for row in fh:
- line = row.strip().split("\n")
- for element in line:
- raw_list.append(element.split(";"))
- #удалить первый элемент в первой строке
- raw_list[0].remove("")
- #создать список наименований продуктов
- product_names = raw_list[0]
- #создать список магазинов с ценами
- shops_with_prices = raw_list[1:]
- #создать список магазинов
- shops = []
- shops += [i[0] for i in shops_with_prices]
- #создать список список цен
- all_prices =[]
- for line in shops_with_prices:
- all_prices.append(list(map(int, line[1:])))
- # найти все значения minimal price
- minimal_prices =[]
- for line in all_prices:
- minimal_prices.append(min(line))
- min_price = min(minimal_prices)
- #создать список продуктов с ценами
- product_list = []
- for i in range(len(product_names)):
- product_prices = []
- product_prices.append(product_names[i])
- for line in all_prices:
- product_prices.append(line[i])
- product_list.append(product_prices)
- #найти все продукты с минимальной ценой и магазины,в которых они продаются.
- best_price_lst = []
- for i in range(len(product_list)):
- for element in product_list[i]:
- if element == min_price:
- best_price_lst.append([product_list[i][0], shops[product_list[i].index(element)-1]])
- #Сортировка
- if len(best_price_lst) == 1:
- print(best_price_lst[0][0], best_price_lst[0][1], sep = "\n")
- elif len(best_price_lst) == 2:
- if best_price_lst[0][0] == best_price_lst[1][0]:
- best_price_lst.sort(key = lambda x: x[1])
- print (best_price_lst[0][0], best_price_lst[0][1], sep = "\n")
- else:
- best_price_lst.sort(key = lambda x: x[0])
- print (best_price_lst[0][0], best_price_lst[0][1], sep = "\n")
- else:
- best_price_lst.sort(key = lambda x: x[0])
- print("Не удалось решить данное условие. Все товары с минимальной ценой в магазинах:\n", best_price_lst)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement