Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("Tervetuloa! Tänään monimutkaiset datarakenteet!")
- # kolmen päivän lämpötilat, lista mikä koostuu listoista (temperatures)
- day_1 = [-11.2, -5.7, -7.8, -8.6]
- day_2 = [0.6, 2.3, 3.7, 1.4]
- day_3 = [3.6, 4.5, 2.7, 3.9]
- temperatures = [day_1, day_2, day_3]
- # print(temperatures)
- for d in temperatures:
- print("Uusi päivä!")
- for temp in d:
- print(temp)
- # UUSI TIEDOSTO
- # dictionary dictionaryn sisällä
- book = {
- "name": "My Lady Jane",
- "year": 2016,
- "publisher": {
- "name": "HarperTeen",
- "organization": "HarperCollins Publishers",
- "location": "New York"
- }
- }
- # kirjan nimi
- book_name = book['name']
- print(book_name)
- # nopea tapa, mutta ei ehkä niin selkeä
- publisher_name = book['publisher']['name']
- print(publisher_name)
- # helpompi tapa, välivaiheilla
- publisher = book['publisher']
- publisher_name = publisher['name']
- print(publisher_name)
- # UUSI TIEDOSTO
- # lista missä on dictionaryjä, eli lista tuotteita!
- products = [
- {"name": "Kahvinkeitin", "price": 129},
- {"name": "Astianpesukone", "price": 299},
- {"name": "Arkkupakastin", "price": 199}
- ]
- # silmukoidaan tuotteet läpi
- for p in products:
- print(p["name"])
- print(f"Hinta: {p['price']}€")
- print()
- # UUSI TIEDOSTO, HOTELLIT
- # esimerkki 1
- 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]
- # print(hotels)
- # print()
- # vd.var_dump(hotels)
- for hotel in hotels:
- print(hotel['name'])
- # perinteinen tapa, sisäkkäiset for-silmukat
- # for service in hotel['services']:
- # print(service)
- # tekstilistoilla voidaan käyttää myös joinia
- # eli yhdistetään kaikki palvelut rivinvaihdolla ja tulostetaan!
- services = "\n".join(hotel['services'])
- print(services)
- print()
- # VERSIO 2, suodatuksia
- sauna_hotels = []
- for hotel in hotels:
- print(hotel['name'])
- # for service in hotel['services']:
- # if service == 'sauna':
- # print("Sauna löytyy!")
- #
- # if service == 'restaurant':
- # print("Hotellissa on ravintola.")
- if "sauna" in hotel['services']:
- sauna_hotels.append(hotel['name'])
- print()
- print(sauna_hotels)
- # UUSI TIEDOSTO, EVENTS RAJAPINTA
- 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)
- choice = input("Millaisia tapahtumia haet?\n")
- for event in data:
- # haetaan osoitetiedot, tulostetaan postinumero + osoite
- address = event['address']
- address_text = f"{address['postal_code']} {address['street_address']}"
- # kategorialista siistiksi pilkulla erotelluksi tekstiksi
- categories = ", ".join(event['categories'])
- if choice not in categories:
- continue
- print(event['name'])
- print(address_text)
- # jos ei ole kategorioita, tulostetaan käyttäjälle viesti
- # muutoin tulostetaan kategoriat
- if categories != "":
- print(categories)
- else:
- print("Ei kategorioita.")
- print()
Add Comment
Please, Sign In to add comment