lundi 2 août 2021

Why does my Python "if-else" block not produce the same result as two if statements with the same logic?

I have a puzzle to solve. There 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).

I need to 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.

Here is the original fr list and d dictionary:


    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',
    '39@stanford.COM|39|82|GIBBO'
    ]
    
    d= {
    '7@comp1.COM': '199',
    '8@comp4.COM': '200',
    '13@comp1.COM': '205'
    }


Here is the first version of my code with two dislodged if statements. This produces the desired results:


    new_list = []
    
    # looping through the fr list items
    for item in fr:
        fr_email = item[ : item.find('|')]  # find the email fr portion
        fr_back = item[item.find('|') :]  # find the back fr portion
    
    # add new key value pair to d - DICTIONARY
    if d.get(fr_email) is None:
        d[fr_email] = str(int(max(d.values())) + 1)
    
    # change the new list to dict value - LIST
    if d.get(fr_email) is not None:
        new_list.append(str(d.get(fr_email)) + fr_back)
    
    fr = new_list # so we don't change the looped list directly

    # prints results:
    # --------------------------------------
    print("Value of fr: ")
    print(fr)
    print("Value of d:")
    print(d)

This is the results for version 1 of my code (two dislodged if-statements):
--------------------------------------
Value of fr: 
['199|4|11|GDSPV', '199|16|82|GDSPV', '205|12|82|GDSPV', '206|19|82|GDSPV', '207|39|82|GIBBO']

Value of d:
{'7@comp1.COM': '199', '8@comp4.COM': '200', '13@comp1.COM': '205', '26@comp1.COM': '206', #'39@stanford.COM': '207'}

This is version 2 of the if statement block (with an else included). The rest remains the same as above.

    
    if d.get(fr_email) is None: # add new key value pair to d - DICTIONARY
        d[fr_email] = str(int(max(d.values())) + 1)
    else: # otherwise add to empty list
        new_list.append(str(d.get(fr_email)) + fr_back)
    
   
Here are the incorrect results, based on the second version. There are fewer list items returned (Value of fr)
--------------------------------------
Value of fr: 
['199|4|11|GDSPV', '199|16|82|GDSPV', '205|12|82|GDSPV']

Value of d:
{'7@comp1.COM': '199', '8@comp4.COM': '200', '13@comp1.COM': '205', '26@comp1.COM': '206', '39@stanford.COM': '207'}

Aucun commentaire:

Enregistrer un commentaire