Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. # решение здесь
  2. #выгрузить данные из файла
  3. raw_list =[]
  4. with open("problem5.csv",encoding="utf8") as fh:
  5. for row in fh:
  6. line = row.strip().split("\n")
  7. for element in line:
  8. raw_list.append(element.split(";"))
  9. #удалить первый элемент в первой строке
  10. raw_list[0].remove("")
  11.  
  12. #создать список наименований продуктов
  13. product_names = raw_list[0]
  14.  
  15. #создать список магазинов с ценами
  16. shops_with_prices = raw_list[1:]
  17.  
  18. #создать список магазинов
  19. shops = []
  20. shops += [i[0] for i in shops_with_prices]
  21.  
  22. #создать список список цен
  23. all_prices =[]
  24. for line in shops_with_prices:
  25. all_prices.append(list(map(int, line[1:])))
  26.  
  27. # найти все значения minimal price
  28. minimal_prices =[]
  29. for line in all_prices:
  30. minimal_prices.append(min(line))
  31. min_price = min(minimal_prices)
  32.  
  33. #создать список продуктов с ценами
  34. product_list = []
  35. for i in range(len(product_names)):
  36. product_prices = []
  37. product_prices.append(product_names[i])
  38. for line in all_prices:
  39. product_prices.append(line[i])
  40. product_list.append(product_prices)
  41.  
  42. #найти все продукты с минимальной ценой и магазины,в которых они продаются.
  43. best_price_lst = []
  44. for i in range(len(product_list)):
  45. for element in product_list[i]:
  46. if element == min_price:
  47. best_price_lst.append([product_list[i][0], shops[product_list[i].index(element)-1]])
  48.  
  49. #Сортировка
  50.  
  51. if len(best_price_lst) == 1:
  52. print(best_price_lst[0][0], best_price_lst[0][1], sep = "\n")
  53.  
  54. elif len(best_price_lst) == 2:
  55. if best_price_lst[0][0] == best_price_lst[1][0]:
  56. best_price_lst.sort(key = lambda x: x[1])
  57. print (best_price_lst[0][0], best_price_lst[0][1], sep = "\n")
  58. else:
  59. best_price_lst.sort(key = lambda x: x[0])
  60. print (best_price_lst[0][0], best_price_lst[0][1], sep = "\n")
  61. else:
  62. best_price_lst.sort(key = lambda x: x[0])
  63. print("Не удалось решить данное условие. Все товары с минимальной ценой в магазинах:\n", best_price_lst)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement