Зміст курсу
Структури Даних в Python
Структури Даних в Python
List Comprehansions with Conditions
You can also use conditions:
For example, suppose you want to create a list of cities located in Japan
.
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] japanese_cities = [] for city in travel_wishlist: if city[1] == "Japan": japanese_cities.append(city[0]) print(japanese_cities) # Output: ['Tokyo', 'Kyoto']
Which is equivalent to:
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] japanese_cities = [city[0] for city in travel_wishlist if city[1] == "Japan"] print(japanese_cities) # Output: ['Tokyo', 'Kyoto']
Swipe to show code editor
Create a new list that contains the names of the cities from your travel_wishlist
, but only if the estimated cost is less than $2500. Use a list comprehension.
Дякуємо за ваш відгук!
List Comprehansions with Conditions
You can also use conditions:
For example, suppose you want to create a list of cities located in Japan
.
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] japanese_cities = [] for city in travel_wishlist: if city[1] == "Japan": japanese_cities.append(city[0]) print(japanese_cities) # Output: ['Tokyo', 'Kyoto']
Which is equivalent to:
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] japanese_cities = [city[0] for city in travel_wishlist if city[1] == "Japan"] print(japanese_cities) # Output: ['Tokyo', 'Kyoto']
Swipe to show code editor
Create a new list that contains the names of the cities from your travel_wishlist
, but only if the estimated cost is less than $2500. Use a list comprehension.
Дякуємо за ваш відгук!
List Comprehansions with Conditions
You can also use conditions:
For example, suppose you want to create a list of cities located in Japan
.
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] japanese_cities = [] for city in travel_wishlist: if city[1] == "Japan": japanese_cities.append(city[0]) print(japanese_cities) # Output: ['Tokyo', 'Kyoto']
Which is equivalent to:
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] japanese_cities = [city[0] for city in travel_wishlist if city[1] == "Japan"] print(japanese_cities) # Output: ['Tokyo', 'Kyoto']
Swipe to show code editor
Create a new list that contains the names of the cities from your travel_wishlist
, but only if the estimated cost is less than $2500. Use a list comprehension.
Дякуємо за ваш відгук!
You can also use conditions:
For example, suppose you want to create a list of cities located in Japan
.
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] japanese_cities = [] for city in travel_wishlist: if city[1] == "Japan": japanese_cities.append(city[0]) print(japanese_cities) # Output: ['Tokyo', 'Kyoto']
Which is equivalent to:
travel_wishlist = [ ["Paris", "France", 2000], ["Tokyo", "Japan", 3000], ["New York", "USA", 2500], ["Kyoto", "Japan", 1500], ["Sydney", "Australia", 4000] ] japanese_cities = [city[0] for city in travel_wishlist if city[1] == "Japan"] print(japanese_cities) # Output: ['Tokyo', 'Kyoto']
Swipe to show code editor
Create a new list that contains the names of the cities from your travel_wishlist
, but only if the estimated cost is less than $2500. Use a list comprehension.