Advertisement
tuomasvaltanen

Untitled

Nov 19th, 2020 (edited)
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.29 KB | None | 0 0
  1. # AC-luento, monimutkaiset tietorakenteet
  2.  
  3. day_1 = [5.6, 7.5, 4.3, 2.1]
  4. day_2 = [0.6, 2.5, 3.3, 5.6]
  5. day_3 = [2.4, 5.3, 4.4, 3.7]
  6.  
  7. temperatures = [day_1, day_2, day_3]
  8.  
  9. for day in temperatures:
  10.     # print(day)
  11.     print()
  12.     for t in day:
  13.         print(t)
  14.  
  15. # UUSI TIEDOSTO, toinen esimerkki
  16.  
  17. books = ["Aapinen", "Da Vinci Code", "Python for Beginners"]
  18. movies = ["Lord of the Rings", "Jurassic Park", "Amadeus"]
  19.  
  20. everything = [books, movies]
  21.  
  22. for item in everything:
  23.     # print(item)
  24.  
  25.     for i in item:
  26.         print(i)
  27.  
  28. # UUSI TIEDOSTO, products esimerkki
  29.  
  30. products = [
  31.     {"name": "Kahvinkeitin", "price": 119},
  32.     {"name": "Astianpesukone", "price": 399},
  33.     {"name": "Arkkupakastin", "price": 199},
  34. ]
  35.  
  36. # print(products)
  37.  
  38. for p in products:
  39.     name = p['name']
  40.     print(name)
  41.  
  42. # UUSI TIEDOSTO
  43.  
  44. # esimerkki, hotelleja
  45.  
  46. import var_dump as vd
  47.  
  48. # luodaan hotelli no. 1
  49. hotel_1 = {
  50.     "name": "Snow Line Hotels",
  51.     "rating": 4.3,
  52.     "wifi": True,
  53.     "free_breakfast": True,
  54.     "services": ["sauna", "meetings", "restaurant", "parking", "safaris"],
  55.     "price_level": 4
  56. }
  57.  
  58. # luodaan hotelli no. 2
  59. hotel_2 = {
  60.     "name": "North Ice Hostel",
  61.     "rating": 3.5,
  62.     "wifi": True,
  63.     "free_breakfast": False,
  64.     "services": ["sauna", "parking"],
  65.     "price_level": 2
  66. }
  67.  
  68. # asetetaan molemmat hotellit samaan listaan
  69. hotels = [hotel_1, hotel_2]
  70.  
  71. # tulostetaan sisältö, käytetään var_dump -moduulia
  72. # vd.var_dump(hotels)
  73. first_hotel = hotels[0]
  74.  
  75. # joko näin
  76. print(hotels[0]['name'])
  77.  
  78. # tai näin
  79. print(first_hotel['name'])
  80.  
  81. # toisen hotellin arvosana
  82. print(hotels[1]['rating'])
  83. print()
  84.  
  85. # jos halutaan tulostaa kaikkien hotellien tiedot
  86. for hotel in hotels:
  87.     print(hotel['name'])
  88.  
  89.  
  90. # jos halutaan tulostaa kaikkien hotellien tiedot
  91. for hotel in hotels:
  92.     print(hotel['name'])
  93.  
  94.     # perinteinen tapa:
  95.     # for service in hotel['services']:
  96.         # print(service)
  97.  
  98.     # jos ei haluta sisäkkäistä for-silmukkaa, voidaan haluttu lopputulos
  99.     # saavuttaa join-funktiolla.
  100.     # logiikka: jokainen hotel-service yhdistetään toisiinsa rivinvaihdolla: \n
  101.     # eli lopputulos on täysin sama kuin tulostaisi jokaisen palvelun erikseen
  102.     # silmukassa
  103.     services = "\n".join(hotel['services'])
  104.     print(services)
  105.     print()
  106.  
  107. # silmukoihin voidaan laittaa if-lauseita lisää jos halutaan:
  108.  
  109. # jos halutaan tulostaa kaikkien hotellien tiedot
  110. for hotel in hotels:
  111.     print(hotel['name'])
  112.  
  113.     # jos hotellissa on sauna, tulostetaan tieto
  114.     if "sauna" in hotel['services']:
  115.         print("Sauna löytyy!")
  116.  
  117.     print()
  118.  
  119. # UUSI TIEDOSTO, INTERNET-DATA
  120.  
  121. import urllib.request
  122. import json
  123.  
  124. # this module needs to be installed separately
  125. # in PyCharm you can install the package if its not found!
  126. import var_dump as vd
  127.  
  128. # get internet data
  129. url = 'https://edu.frostbit.fi/api/events'
  130.  
  131. req = urllib.request.Request(url)
  132. raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
  133.  
  134. # tarvittava data on nyt data-muuttujassa!
  135. data = json.loads(raw_data)
  136. vd.var_dump(data)
  137.  
  138. # kaikki-tapahtumat (data) -> 7 (kahdeksas) -> osoitetiedot -> katuosoite
  139. # first_event = data[7]['address']['street_address']
  140. # print(first_event)
  141. print()
  142. for event in data:
  143.     print(event['name'])
  144.  
  145.     categories = ", ".join(event['categories'])
  146.  
  147.     if categories != "":
  148.         print(categories)
  149.     else:
  150.         print("Tapahtumalla ei ole kategorioita.")
  151.  
  152.     print()
  153.  
  154. # ESIMERKKI 2, SUODATETAAN TAPAHTUMIA KATEGORIAN PERUSTEELLA
  155.  
  156. print()
  157.  
  158. choice = input("Anna kategoria:\n")
  159.  
  160. for event in data:
  161.     categories = ", ".join(event['categories'])
  162.  
  163.     # jos valitsemaamme kategoriaa ei löydy,
  164.     # skipataan tämä kierros ja jatketaan silmukkaa
  165.     # seuraavasta kierroksesta
  166.     if choice not in categories:
  167.         continue
  168.  
  169.     # tulostetaan nimi vasta continuen jälkeen
  170.     # ettei turhaan tulosteta sellaisten tapahtumien nimiä
  171.     # jotia ei pidä tulostaa
  172.     print(event['name'])
  173.  
  174.     # tulostetaan myös osoitetiedot
  175.     event_street = event['address']['street_address']
  176.     event_postal_code = event['address']['postal_code']
  177.     print(f"{event_street} {event_postal_code}")
  178.  
  179.     if categories != "":
  180.         print(categories)
  181.     else:
  182.         print("Tapahtumalla ei ole kategorioita.")
  183.  
  184.     print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement