-
Notifications
You must be signed in to change notification settings - Fork 0
/
64_dictionary_comprehension.py
31 lines (22 loc) · 1.13 KB
/
64_dictionary_comprehension.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
# dictionary comprehension = create dictionary using an expression
# can replace for loops and certain lambda functions
# dictionary = {key: expresion for (key,value) in iterable}
# dictionary = {key: expresion for (key,value) in iterable if contitional}
# dictionary = {key: (if/else0) expresion for (key,value) in iterable}
cities_in_F = {'New York': 32, 'Boston': 75, 'Los Angeles': 100, 'chicago': 50}
cities_in_C = {key: round((value-32)*(5/9)) for (key,value) in cities_in_F.items()}
print(cities_in_C)
weather = {'New York': "snowing", 'Boston': "sunny", 'Los Angeles': "sunny", 'Chicago': "cloudy"}
sunny_weather = {key: value for (key,value) in weather.items() if value == "sunny"}
print(sunny_weather)
def check_temp(value):
if value >= 70:
return "HOT"
elif 69>= value >= 40:
return "COLD"
else:
return "COLD"
cities = {'New York': 32, 'Boston': 75, 'Los Angeles': 100, 'chicago': 50}
# desc_cities = {key: ("WARM" if value >= 40 else "COLD") for (key,value) in cities.items()}
desc_cities = {key: check_temp(value) for (key,value) in cities.items()}
print(desc_cities)