Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # AC-luento, monimutkaiset tietorakenteet
- day_1 = [5.6, 7.5, 4.3, 2.1]
- day_2 = [0.6, 2.5, 3.3, 5.6]
- day_3 = [2.4, 5.3, 4.4, 3.7]
- temperatures = [day_1, day_2, day_3]
- for day in temperatures:
- # print(day)
- print()
- for t in day:
- print(t)
- # UUSI TIEDOSTO, toinen esimerkki
- books = ["Aapinen", "Da Vinci Code", "Python for Beginners"]
- movies = ["Lord of the Rings", "Jurassic Park", "Amadeus"]
- everything = [books, movies]
- for item in everything:
- # print(item)
- for i in item:
- print(i)
- # UUSI TIEDOSTO, products esimerkki
- products = [
- {"name": "Kahvinkeitin", "price": 119},
- {"name": "Astianpesukone", "price": 399},
- {"name": "Arkkupakastin", "price": 199},
- ]
- # print(products)
- for p in products:
- name = p['name']
- print(name)
- # UUSI TIEDOSTO
- # esimerkki, hotelleja
- import var_dump as vd
- # luodaan hotelli no. 1
- hotel_1 = {
- "name": "Snow Line Hotels",
- "rating": 4.3,
- "wifi": True,
- "free_breakfast": True,
- "services": ["sauna", "meetings", "restaurant", "parking", "safaris"],
- "price_level": 4
- }
- # luodaan hotelli no. 2
- hotel_2 = {
- "name": "North Ice Hostel",
- "rating": 3.5,
- "wifi": True,
- "free_breakfast": False,
- "services": ["sauna", "parking"],
- "price_level": 2
- }
- # asetetaan molemmat hotellit samaan listaan
- hotels = [hotel_1, hotel_2]
- # tulostetaan sisältö, käytetään var_dump -moduulia
- # vd.var_dump(hotels)
- first_hotel = hotels[0]
- # joko näin
- print(hotels[0]['name'])
- # tai näin
- print(first_hotel['name'])
- # toisen hotellin arvosana
- print(hotels[1]['rating'])
- print()
- # jos halutaan tulostaa kaikkien hotellien tiedot
- for hotel in hotels:
- print(hotel['name'])
- # jos halutaan tulostaa kaikkien hotellien tiedot
- for hotel in hotels:
- print(hotel['name'])
- # perinteinen tapa:
- # for service in hotel['services']:
- # print(service)
- # jos ei haluta sisäkkäistä for-silmukkaa, voidaan haluttu lopputulos
- # saavuttaa join-funktiolla.
- # logiikka: jokainen hotel-service yhdistetään toisiinsa rivinvaihdolla: \n
- # eli lopputulos on täysin sama kuin tulostaisi jokaisen palvelun erikseen
- # silmukassa
- services = "\n".join(hotel['services'])
- print(services)
- print()
- # silmukoihin voidaan laittaa if-lauseita lisää jos halutaan:
- # jos halutaan tulostaa kaikkien hotellien tiedot
- for hotel in hotels:
- print(hotel['name'])
- # jos hotellissa on sauna, tulostetaan tieto
- if "sauna" in hotel['services']:
- print("Sauna löytyy!")
- print()
- # UUSI TIEDOSTO, INTERNET-DATA
- import urllib.request
- import json
- # this module needs to be installed separately
- # in PyCharm you can install the package if its not found!
- import var_dump as vd
- # get internet data
- url = 'https://edu.frostbit.fi/api/events'
- req = urllib.request.Request(url)
- raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
- # tarvittava data on nyt data-muuttujassa!
- data = json.loads(raw_data)
- vd.var_dump(data)
- # kaikki-tapahtumat (data) -> 7 (kahdeksas) -> osoitetiedot -> katuosoite
- # first_event = data[7]['address']['street_address']
- # print(first_event)
- print()
- for event in data:
- print(event['name'])
- categories = ", ".join(event['categories'])
- if categories != "":
- print(categories)
- else:
- print("Tapahtumalla ei ole kategorioita.")
- print()
- # ESIMERKKI 2, SUODATETAAN TAPAHTUMIA KATEGORIAN PERUSTEELLA
- print()
- choice = input("Anna kategoria:\n")
- for event in data:
- categories = ", ".join(event['categories'])
- # jos valitsemaamme kategoriaa ei löydy,
- # skipataan tämä kierros ja jatketaan silmukkaa
- # seuraavasta kierroksesta
- if choice not in categories:
- continue
- # tulostetaan nimi vasta continuen jälkeen
- # ettei turhaan tulosteta sellaisten tapahtumien nimiä
- # jotia ei pidä tulostaa
- print(event['name'])
- # tulostetaan myös osoitetiedot
- event_street = event['address']['street_address']
- event_postal_code = event['address']['postal_code']
- print(f"{event_street} {event_postal_code}")
- if categories != "":
- print(categories)
- else:
- print("Tapahtumalla ei ole kategorioita.")
- print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement