Repository navigation
Expand file tree
/
Copy path01_zip_dictionary_mapping_solution.py
More file actions
130 lines (93 loc) · 3.04 KB
/
Copy path01_zip_dictionary_mapping_solution.py
File metadata and controls
130 lines (93 loc) · 3.04 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""
# 2. The Challenge
users = ['ava', 'sam', 'mia']
roles = ['admin', 'editor', 'viewer']
# Build user_to_role, then find all users who can edit content.
user_to_role = {
'ava' : ['admin', 'editor'],
'sam' : ['editor', 'viewer'],
'mia' : ['viewer']
}
editor = [
user
for user, user_role in user_to_role.items()
if "editor" in user_role
]
print(editor)
"""
#-----------------------------------------------------------------------------------------#
'''
# 3. My Current Python Toolkit
# zip pairs related values; dict stores fast key -> value lookups; a loop filters results.
users = ["ava", "sam", "mia", "leo"]
scores = [85, 62, 91, 74]
""" Your tasks:
1. Use zip() to pair each user with their score.
2. Build a dictionary called user_scores.
3. Find all users who scored at least 80.
4. Store their names in a list called qualified_users. """
""" instead
user_scores = {
"ava" : 85,
"sam" :62,
"mia" : 74,
"leo" : 91
} """
user_scores =dict(zip(users, scores))
ans = {
user:user_val
for user,user_val in user_scores.items() if user_val >= 80
}
print(user_scores)
print(ans)
'''
#-----------------------------------------------------------------------------------------#
""" prints only ['ava', 'sam']
#zip stops as soon as iterator run out of element
#if still want to take mia with null
from itertools import zip_longest
users = ['ava', 'sam', 'mia']
roles = ['admin', 'editor']
user_to_role = dict(
zip_longest(users, roles, fillvalue=None)
)
print(user_to_role)
"""
#-----------------------------------------------------------------------------------------#
# 5 same concpets
#-----------------------------------------------------------------------------------------#
# Q1. What happens when roles has fewer items than users?
# Use the example in Section 4 to determine the result.
#
# Q2. What happens when two users have the same role?
# Use the example in Section 4 to explain the behavior.
#
# Q3. Why is a dictionary better than repeatedly scanning two lists?
# Compare looking up a user's role in a dictionary with
# finding the user's index in a list and accessing the role
# at the same index.
#
# Q4. How would you represent a user with multiple roles?
# Assume Ava can be both an admin and a viewer.
# Write the dictionary entry that represents this.
#
# Q5. Does this mapping alone separate Tenant A from Tenant B?
# Assume both tenants can have a user named 'sam'.
# Explain why a username-only mapping cannot distinguish them.
#1
""" users = ['ava', 'sam', 'mia']
roles = ['admin', 'editor']
mapping = dict(zip(users, roles))
print(mapping) """
#output = {'ava': 'admin', 'sam': 'editor'}
#2
""" users = ['ava', 'sam']
roles = ['editor', 'editor']
mapping = dict(zip(users, roles)) """
#output 2 = {'ava': 'editor', 'sam': 'editor'}
#3
""" users = ['ava', 'ava']
roles = ['admin', 'viewer']
print(dict(zip(users, roles))) """
#output ={'ava': 'viewer'}, However, if the same username appears twice with different roles,
# the later value overwrites the earlier one