-
Notifications
You must be signed in to change notification settings - Fork 0
/
DictCompreHension.py
62 lines (42 loc) · 1.87 KB
/
DictCompreHension.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
# Dictionary Comprehension = create dictionaries using an expression can replace for loops
# and certain lambda fucntion
# dictionary = {key: expression for (key,value) in iterable}
# dictionary = {key: expression for (key,value) in iterable if conditional }
# dictionary = {key: (if/else) for (key,value) in iterable }
# dictionary = {key: function(value) for (key,value) in iterable }
# cities_temperture = {'Bhubaneswar':34,'Bhadrak':36,'Cuttack':30,'Anugul':23}
# print(cities_temperture)
# print('-' * 50)
# cities_temperture_in_F = {key: round((value -32) * (5/9)) for (key,value) in cities_temperture.items()}
# print(cities_temperture_in_F)
# print('-' * 50)
# -----------------------------------------------------------------
# weather = {'Bhubaneswar':'cloudy','Bhadrak':'snowing','Cuttack':'rain','Anugul':'cloudy'}
#
# rainy_cities = {key: value for key,value in weather.items() if value == 'rain'}
# cloudy_cities = {key: value for key,value in weather.items() if value == 'cloudy'}
# snowing_cities = {key:value for key,value in weather.items() if value == 'snowing'}
#
#
# print(snowing_cities)
# print(cloudy_cities)
# print(rainy_cities)
# ---------------------------------------------------------
# cities_temp = {'Bhubaneswar':34,'Bhadrak':24,'Cuttack':45,'Anugul':40}
#
# Helthy_weather = {key: ('Good'if value <= 30 else 'bad' ) for (key,value) in cities_temp.items()}
# print(Helthy_weather)
#
# warm_cold = {key:('cold' if value <= 30 else 'warm') for (key,value) in cities_temp.items()}
# print(warm_cold)
# ------------------------------------------------
cities_temp = {'Bhubaneswar':34,'Bhadrak':24,'Cuttack':45,'Anugul':40}
def add(temp):
if temp >= 30:
temp = temp - 5
else:
temp += 2
return temp
add_temp = {key: add(value) for (key,value) in cities_temp.items()}
print(add_temp)
print(cities_temp)