forked from imtiazahmad007/PythonCourse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassignment_09.py
114 lines (59 loc) · 2 KB
/
assignment_09.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
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
# Assignment 9
"""
We have 2 variables. fr and d. fr is a list of strings and d is a dictionary with email
addresses as keys and numbers as values (numbers in string format).
Write code to replace the email address in each of the strings in the fr list with
the associated value of that email looked up from the dictionary d.
If the dictionary does not contain the email found in the list, add a new entry
in the dictionary for the email found in the fr list. The value for this new email key
will be the next highest value number in the dictionary in string format.
Once the dictionary is populated with this new email key and a new number value,
replace that email's occurrence in the fr list with the number value.
The output of running your completed code should be the following:
Value of fr:
['199|4|11|GDSPV', '199|16|82|GDSPV', '205|12|82|GDSPV', '206|19|82|GDSPV']
Value of d:
{'7@comp1.COM': '199', '8@comp4.COM': '200', '13@comp1.COM': '205', '26@comp1.COM': '206'}
"""
# Don't manually change fr and d.
fr = [
'7@comp1.COM|4|11|GDSPV',
'7@comp1.COM|16|82|GDSPV',
'13@comp1.COM|12|82|GDSPV',
'26@comp1.COM|19|82|GDSPV'
]
d= {
'7@comp1.COM': '199',
'8@comp4.COM': '200',
'13@comp1.COM': '205'
}
# Your Code Below:
# --------------------------------------
# don't change the lines below:
# --------------------------------------
print("Value of fr: ")
print(fr)
print("Value of d:")
print(d)
# Solution:
#
# line_list = []
# # this won't work for missing keys
# for line in fr:
# columns = line.split("|")
# lookup_val = columns[0]
#
# # if lookup_val not in d.keys():
# if(d.get(lookup_val) is None): # Can't find in dict
#
# # get the next number + 1 from dict
# next_number = int(max(d.values())) + 1
# d[lookup_val] = str(next_number)
# columns[0] = str(next_number)
# line_list.append("|".join(columns))
# else:
# columns[0] = d.get(columns[0])
# line_list.append("|".join(columns))
#
# fr = line_list
#