tuomasvaltanen

Untitled

Nov 24th, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.00 KB | None | 0 0
  1. # complex datasets and how to work with them
  2.  
  3. day1 = [13.4, 16.5, 15.2, 18.6]
  4. day2 = [11.7, 15.3, 11.7, 13.4]
  5. day3 = [12.9, 16.2, 14.2, 15.5]
  6.  
  7. # list of lists
  8. # measurement days in a list
  9. temperatures = [day1, day2, day3]
  10.  
  11. # loop through each day in the list
  12. for day in temperatures:
  13.     print("New day!")
  14.  
  15.     # loop through each temperature in one day at a time
  16.     for temp in day:
  17.         print(temp)
  18.  
  19.     print()
  20.  
  21. # NEW FILE
  22.  
  23. # this is a possible scenario, each product is separated
  24. # into another list based no their category
  25. movies = ["Amadeus", "Jurassic Park", "Lord of the Rings"]
  26. books = ["Da Vinci Code", "Robinson Crusoe", "Python for Beginners"]
  27.  
  28. # list of lists
  29. products = [movies, books]
  30.  
  31. # loop through each category-list
  32. for category in products:
  33.  
  34.     # loop through each product in one category
  35.     for item in category:
  36.         print(item)
  37.  
  38. # NEW FILE
  39.  
  40. # dictionary inside another dictionary
  41. book = {
  42.     "name":  "My Lady Jane",
  43.     "year":  2016,
  44.     "publisher": {
  45.         "name": "HarperTeen",
  46.         "organization": "HarperCollins Publishers",
  47.         "location": "New York"
  48.     }
  49. }
  50.  
  51. print(book['name'])
  52. print(book['year'])
  53.  
  54. # the straightforward way with string variable
  55. print(book['publisher']['location'])
  56.  
  57. # if it's easier to use, this is also okay
  58. publisher = book['publisher']
  59. location = publisher['location']
  60. print(location)
  61.  
  62. # NEW FILE
  63.  
  64. # the most typical combination you get from the internet data,
  65. # a list of dictionaries
  66.  
  67. products = [
  68.     {"name": "Coffee maker", "price": 79},
  69.     {"name": "Dishwasher", "price": 299},
  70.     {"name": "Freezer", "price": 199}
  71. ]
  72.  
  73. # go through each product one at a time
  74. for item in products:
  75.     # print(item['name'])
  76.     # print(item['price'])
  77.  
  78.     print(f"{item['name']}, {item['price']} €")
  79.  
  80. # NEW FILE
  81.  
  82. # example 1
  83.  
  84. # remember to install var_dump first in your PyCharm-project
  85. import var_dump as vd
  86.  
  87. # create first hotel
  88. hotel_1 = {
  89.     "name": "Snow Line Hotels",
  90.     "rating": 4.3,
  91.     "wifi": True,
  92.     "free_breakfast": True,
  93.     "services": ["sauna", "meetings", "restaurant", "parking", "safaris"],
  94.     "price_level": 4
  95. }
  96.  
  97. # create second hotel
  98. hotel_2 = {
  99.     "name": "North Ice Hostel",
  100.     "rating": 3.5,
  101.     "wifi": True,
  102.     "free_breakfast": False,
  103.     "services": ["sauna", "parking"],
  104.     "price_level": 2
  105. }
  106.  
  107. # place both hotels into one list
  108. hotels = [hotel_1, hotel_2]
  109.  
  110. first_hotel = hotels[0]
  111.  
  112. # vd.var_dump(first_hotel)
  113.  
  114. # this is used by the loop to filter out
  115. # suitable hotels (that have a sauna)
  116. sauna_hotels = []
  117.  
  118. # loop through each hotel in the list (we have now 2 hotels,
  119. # we could have hundreds if this originated from the internet)
  120. for hotel in hotels:
  121.     print(hotel['name'])
  122.     print("Available services:")
  123.  
  124.     # this is the traditional way to go through a list of text
  125.     # for service in hotel['services']:
  126.     #     print(service)
  127.  
  128.     # with list of text, you can do this also:
  129.     services = "\n".join(hotel['services'])
  130.     # print(services)
  131.  
  132.     # print this extra info if hotel has a sauna
  133.     if "sauna" in services:
  134.         print("This hotel has a sauna.")
  135.         sauna_hotels.append(hotel['name'])
  136.  
  137.     print()
  138.  
  139. # just check quickly what is inside our filtered list
  140. print(sauna_hotels)
  141.  
  142. # NEW FILE, DATA FROM INTERNET
  143.  
  144. import urllib.request
  145. import json
  146.  
  147. # this module needs to be installed separately
  148. # in PyCharm you can install the package if its not found!
  149. import var_dump as vd
  150.  
  151. # get internet data
  152. url = 'https://edu.frostbit.fi/api/events/en'
  153. req = urllib.request.Request(url)
  154. raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
  155.  
  156. # all events are now in data-variable!
  157. data = json.loads(raw_data)
  158.  
  159. # ask the user for a search word, let's make a small event search engine
  160. search_word = input("What events are you looking for?\n")
  161.  
  162. # loop through all event data what happens to be in the internet today
  163. for event in data:
  164.     address_text = event['address']['street_address']
  165.     postal_code = event['address']['postal_code']
  166.  
  167.     # category list into a string
  168.     categories = ", ".join(event['categories'])
  169.  
  170.     # using continue here is neat
  171.     # if the search word is not found in the categories
  172.     # => skip the whole event with continue-command
  173.     if search_word not in categories:
  174.         continue
  175.  
  176.     # print all event text
  177.     print(event['name'])
  178.     print(f"Address: {address_text} {postal_code}")
  179.     print(categories)
  180.  
  181.     print()
  182.  
  183. # NEW FILE
  184.  
  185. import json
  186. import urllib.request
  187. import var_dump as vd
  188.  
  189. url = "https://edu.frostbit.fi/api/weather/"
  190. req = urllib.request.Request(url)
  191. raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
  192. weather = json.loads(raw_data)
  193.  
  194. # modify these as needed in the loop (if-statements and so)
  195. strongest_wind = 0
  196. weakest_wind = 0
  197. strongest_wind_city = ""
  198. weakest_wind_city = ""
  199.  
  200. # go through each city and their weather conditions
  201. for city in weather:
  202.     print(city['location'])
  203.     print(city['wind'])
  204.     print()
  205.  
  206.  
  207.  
Add Comment
Please, Sign In to add comment