Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

python - Split list of emails and numbers, construct dictionary with emails as keys and auto-incremented numeric values

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'}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's a complete solution:

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'
}

# Figure out the highest key value in the `d` dictionary and set `next_id` to be one greater than that
next_id = -1
for id in d.values():
    if int(id) > next_id:
        next_id = int(id)
next_id += 1

# Create the start of the list we're going to build up
r = []

# For each input in `fr`...
for line in fr:

    # Split the input into elements
    elements = line.split('|')

    # Extract the email address
    email = elements[0]

    # Is this address in `d`?
    if email not in d:
        # No, so add it with the next id as its value
        d[email] = str(next_id)
        next_id += 1

    # Replace the email element with the value for that email from `d`
    elements[0] = d[email]

    # Concatenate the elements back together and put the resulting string in our results list `r`
    r.append('|'.join(elements))

# Print our three structures
print(f"Value of fr: {fr}")
print(f"Value of d: {d}")
print(f"Value of r: {r}")

Result:

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'}
Value of r: ['199|4|11|GDSPV', '199|16|82|GDSPV', '205|12|82|GDSPV', '206|19|82|GDSPV']

Notice that we don't have to know what any of the email addresses are. We just process whatever we find. I wasn't sure what "the next highest value number in the dictionary " meant, so maybe what I did to come up with the next value to use in the dictionary needs to be changed if my interpretation of that is incorrect.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...