Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # complex datasets and how to work with them
- day1 = [13.4, 16.5, 15.2, 18.6]
- day2 = [11.7, 15.3, 11.7, 13.4]
- day3 = [12.9, 16.2, 14.2, 15.5]
- # list of lists
- # measurement days in a list
- temperatures = [day1, day2, day3]
- # loop through each day in the list
- for day in temperatures:
- print("New day!")
- # loop through each temperature in one day at a time
- for temp in day:
- print(temp)
- print()
- # NEW FILE
- # this is a possible scenario, each product is separated
- # into another list based no their category
- movies = ["Amadeus", "Jurassic Park", "Lord of the Rings"]
- books = ["Da Vinci Code", "Robinson Crusoe", "Python for Beginners"]
- # list of lists
- products = [movies, books]
- # loop through each category-list
- for category in products:
- # loop through each product in one category
- for item in category:
- print(item)
- # NEW FILE
- # dictionary inside another dictionary
- book = {
- "name": "My Lady Jane",
- "year": 2016,
- "publisher": {
- "name": "HarperTeen",
- "organization": "HarperCollins Publishers",
- "location": "New York"
- }
- }
- print(book['name'])
- print(book['year'])
- # the straightforward way with string variable
- print(book['publisher']['location'])
- # if it's easier to use, this is also okay
- publisher = book['publisher']
- location = publisher['location']
- print(location)
- # NEW FILE
- # the most typical combination you get from the internet data,
- # a list of dictionaries
- products = [
- {"name": "Coffee maker", "price": 79},
- {"name": "Dishwasher", "price": 299},
- {"name": "Freezer", "price": 199}
- ]
- # go through each product one at a time
- for item in products:
- # print(item['name'])
- # print(item['price'])
- print(f"{item['name']}, {item['price']} €")
- # NEW FILE
- # example 1
- # remember to install var_dump first in your PyCharm-project
- import var_dump as vd
- # create first hotel
- hotel_1 = {
- "name": "Snow Line Hotels",
- "rating": 4.3,
- "wifi": True,
- "free_breakfast": True,
- "services": ["sauna", "meetings", "restaurant", "parking", "safaris"],
- "price_level": 4
- }
- # create second hotel
- hotel_2 = {
- "name": "North Ice Hostel",
- "rating": 3.5,
- "wifi": True,
- "free_breakfast": False,
- "services": ["sauna", "parking"],
- "price_level": 2
- }
- # place both hotels into one list
- hotels = [hotel_1, hotel_2]
- first_hotel = hotels[0]
- # vd.var_dump(first_hotel)
- # this is used by the loop to filter out
- # suitable hotels (that have a sauna)
- sauna_hotels = []
- # loop through each hotel in the list (we have now 2 hotels,
- # we could have hundreds if this originated from the internet)
- for hotel in hotels:
- print(hotel['name'])
- print("Available services:")
- # this is the traditional way to go through a list of text
- # for service in hotel['services']:
- # print(service)
- # with list of text, you can do this also:
- services = "\n".join(hotel['services'])
- # print(services)
- # print this extra info if hotel has a sauna
- if "sauna" in services:
- print("This hotel has a sauna.")
- sauna_hotels.append(hotel['name'])
- print()
- # just check quickly what is inside our filtered list
- print(sauna_hotels)
- # NEW FILE, DATA FROM INTERNET
- 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/en'
- req = urllib.request.Request(url)
- raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
- # all events are now in data-variable!
- data = json.loads(raw_data)
- # ask the user for a search word, let's make a small event search engine
- search_word = input("What events are you looking for?\n")
- # loop through all event data what happens to be in the internet today
- for event in data:
- address_text = event['address']['street_address']
- postal_code = event['address']['postal_code']
- # category list into a string
- categories = ", ".join(event['categories'])
- # using continue here is neat
- # if the search word is not found in the categories
- # => skip the whole event with continue-command
- if search_word not in categories:
- continue
- # print all event text
- print(event['name'])
- print(f"Address: {address_text} {postal_code}")
- print(categories)
- print()
- # NEW FILE
- import json
- import urllib.request
- import var_dump as vd
- url = "https://edu.frostbit.fi/api/weather/"
- req = urllib.request.Request(url)
- raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
- weather = json.loads(raw_data)
- # modify these as needed in the loop (if-statements and so)
- strongest_wind = 0
- weakest_wind = 0
- strongest_wind_city = ""
- weakest_wind_city = ""
- # go through each city and their weather conditions
- for city in weather:
- print(city['location'])
- print(city['wind'])
- print()
Add Comment
Please, Sign In to add comment