-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path*Intro_Dictionary.py
More file actions
72 lines (58 loc) · 2.19 KB
/
*Intro_Dictionary.py
File metadata and controls
72 lines (58 loc) · 2.19 KB
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
63
64
65
66
67
68
69
70
71
72
# Reference:
# https://science.nasa.gov/climate-change/effects/
# https://github.com/SciComp8/Python_Programming/blob/main/Utilities/Function/Lambda_Function.py
# Create a dictionary
my_dict = {}
climate_change_impact = {
'Climate': ['global temperatures rise', 'sea level rise', 'glaciers are shrinking', 'carbon dioxide rises', 'snow is melting earlier'],
'Food': ['challenges for farming', 'heat-related health issues', 'harm livestock'],
'Water': ['flooding', 'stronger and more frequent abnormally heavy precipitation events', 'drought', 'plants need more water', 'less snow overall'],
'Infrastructure': ['more indoor cooling', 'flooding shuts down highways and major business areas', 'sea level rise affects coastal infrastructure (e.g., roads, bridges, water supplies)']
}
my_yogurt = {
'kefir': 6.9,
'filmjolk': 4.6,
'regular yogurt': 3.7
}
print(climate_change_impact['Infrastructure'])
print(climate_change_impact['Infrastructure'][0])
print(my_yogurt['filmjolk'])
print(my_yogurt.get['filmjolk2']) # None
def make_dict(**kwargs):
return kwargs
make_dict(alpha = 1, beta = 2, gamma = 6)
# {'alpha': 1, 'beta': 2, 'gamma': 6}
# Obtain items
print(my_yogurt.items())
# dict_items([('kefir', 6.9), ('filmjolk', 4.6), ('regular yogurt', 3.7)])
print(my_yogurt.keys())
# dict_keys(['kefir', 'filmjolk', 'regular yogurt'])
print(my_yogurt.values())
# dict_values([6.9, 4.6, 3.7])
# Add new item
my_yogurt['filmjolk2': 5.6]
print(my_yogurt.items())
# Modify the value
my_yogurt['regular yogurt'] = 3.5
print(my_yogurt['regular yogurt'])
# Delete a dictionary
del my_yogurt
# Remove a key-value pair
del my_yogurt['regular yogurt']
# Empty a dictionary
my_yogurt.clear()
# Iterate a dictionary
for key, value in my_yogurt.items():
print(f'\nproduct: {key}')
print(f'price: {value}')
for key, value in my_yogurt.items():
print(f"The price of the {key} is {value}.")
# Iterate the key
for key in my_yogurt:
print(f'\nproduct: {key}')
# Iterate the value
for value in my_yogurt.values():
print(f'\nprice: {value}')
# Sort a dictionary by value
# Pass a lambda functions as an argument into the sorted function
print(dict(sorted(my_dict.items(), key=lambda item : item[1]))) # argument(s) : expression