I've been struggling with this assignment for a few days and can't figure out how to write proper pythonic code to replace the values in the lists when there are pipes in the list strings.
- 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.
- Don't manually change
fr
and d
.
Sample input:
fr = [
'[email protected]|4|11|GDSPV',
'[email protected]|16|82|GDSPV',
'[email protected]|12|82|GDSPV',
'[email protected]|19|82|GDSPV'
]
d = {
'[email protected]': '199',
'[email protected]': '200',
'[email protected]': '205'
}
The assignment gives what the output should look like, but I'm struggling to get there because of the pipes:
Value of fr:
['199|4|11|GDSPV', '199|16|82|GDSPV', '205|12|82|GDSPV', '206|19|82|GDSPV']
Value of d:
{'[email protected]': '199', '[email protected]': '200', '[email protected]': '205', '[email protected]': '206'}
This is what the assignment gives you to start off:
line_list = []
for line in fr:
And this is what I have so far:
line_list = []
for line in fr:
pipes = line.split('|')
if pipes[0] == '[email protected]':
pipes[0] = d['[email protected]']
elif pipes[0] == '[email protected]':
pipes[0] = d['[email protected]']
elif pipes[0] == '[email protected]':
pipes[0] = d['[email protected]']
print(pipes)
if len(d) < 4:
d['[email protected]'] = '206'
print("Value of fr: ")
print(fr)
print("Value of d:")
print(d)
Which outputs:
['199', '4', '11', 'GDSPV']
['199', '16', '82', 'GDSPV']
['205', '12', '82', 'GDSPV']
['206', '19', '82', 'GDSPV']
Value of fr:
['[email protected]|4|11|GDSPV', '[email protected]|16|82|GDSPV', '[email protected]|12|82|GDSPV', '[email protected]|19|82|GDSPV']
Value of d:
{'[email protected]': '199', '[email protected]': '200', '[email protected]': '205', '[email protected]': '206'}