-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChapter 60 - DictionaryComprehensions.py
62 lines (43 loc) · 2.04 KB
/
Chapter 60 - DictionaryComprehensions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# CHAPTER 60
# Dictionary Comprehensions = create dictionaries using an expression
# can replace for loops and certain lambda functions
# PART 1
# dictionary = {key: expression for (key, value) in iterable}
cities_in_F = {'New York': 32, 'Boston': 75, 'Los Angeles': 100, 'Chicago': 50}
# First, it gets all the items in cities_in_F dictionary
# Next, it iterates and gets the key-value pair inside each item
# Then, the values are being applied with formula
# to convert Fahrenheit to Celsius
cities_in_C = {key: round((value -32) * 5/9) for (key, value) in cities_in_F.items()}
print(cities_in_C)
# PART 2
# dictionary = {key: expression for (key, value) in iterable if conditional}
weather = {'New York': "snowing", 'Boston': "sunny", 'Los Angeles': "sunny", 'Chicago': 'cloudy'}
# First, it gets all the items in weather dictionary
# Next, it iterates and gets the key-value pair inside each item
# Then, the value is checked for every item if it is sunny
# IF it is sunny include the item in the sunny_states dictionary
sunny_states = {key: value for (key, value) in weather.items() if value == "sunny"}
print(sunny_states)
# PART 3
# dictionary = {key: (if/else) for (key, value) in iterable}
cities = {'New York': 32, 'Boston': 75, 'Los Angeles': 100, 'Chicago': 50}
# IF the value of items in cities is greater than 40, assign WARM
# ELSE, assign COLD
cities_status = {key: ("WARM" if value >= 40 else "COLD") for (key, value) in cities.items()}
print(cities_status)
# PART 4
# dictionary = {key: function(value) for (key,value) in iterable}
def check_temp(value):
if value >= 70:
return "HOT"
elif 69 >= value >= 40:
return "WARM"
else:
return "COLD"
cities = {'New York': 32, 'Boston': 75, 'Los Angeles': 100, 'Chicago': 50}
# Depending on the value of the item in cities dictionary
# It will call a function check_temp() to apply different conditions
# and return corresponding values
cities_status = {key: check_temp(value) for (key, value) in cities.items()}
print(cities_status)