Home > Blockchain >  Returning Specific Split Strings in a Dictionary from a List
Returning Specific Split Strings in a Dictionary from a List

Time:11-20

This is an exercise in a class I am taking. I posted this to the message board and the feedback I was given stated that I am only working with the last name in a list of names. Ultimately, when this runs, it only returns {Marin:1} whereas it should return a dictionary containing all first names as the key and the value is the number of times the name appears in a list. Any and all help is appreciated.

#Write a function called name_counts. name_counts will take
#as input a list of full names. Each name will be two words
#separated by a space, like "David Joyner".
#
#The function should return a dictionary. The keys to the
#dictionary will be the first names from the list, and the
#values should be the number of times that first name
#appeared.
#
#HINT: Use split() to split names into first and last.


#Add your function here!

    def name_counts(name_list):
        
        nameDict = {}
        for name in name_list:
            name = name.split()
            new_name = name[0]
        for first_name in new_name:
            if first_name in nameDict:
                nameDict[new_name] =1
            else:
                nameDict[new_name]=1
        return nameDict

#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print (although the order of the keys may vary):
#{'Shelba': 5, 'Maren': 1, 'Nicol': 1, 'David': 2, 'Brenton': 2}

    name_list = ["David Joyner", "David Zuber", "Brenton Joyner",
                 "Brenton Zuber", "Nicol Barthel", "Shelba Barthel",
                 "Shelba Crowley", "Shelba Fernald", "Shelba Odle",
                 "Shelba Fry", "Maren Fry"]
    print(name_counts(name_list))

CodePudding user response:

the issue is the two for loops.

for name in name_list:
            name = name.split()
            new_name = name[0]

The first for loop iterates through the list of names, spliting them and assigning the first name to new_name When the for loop is done, the value of new_name is "Maren" which is the last first name in the list. The second for loop won't start till the first one is finished, as such it only knows the value of new_name as "Maren"

.

for first_name in new_name:
            if first_name in nameDict:
                nameDict[new_name] =1
            else:
                nameDict[new_name]=1

The next for loop iterates over the name "Maren" which is a string so it does it one character at a time As none of the characters "M", "a", "r", "e", "n" are in the dictionary it defaults to the else clause which is setting the value of the key "Maren" to 1 each time. That's the reason you get {"Maren": 1}

Remove the second for loop and simply check if new_name is in nameDict under the first for loop.

CodePudding user response:

You were so close you had all the right ideas, but you're doing too much. You can just keep adding 1 whenever you get a name. It's very simple to do if you use the get method for dictionaries.

def name_counts(name_list):

    nameDict = {}
    for name in name_list:
        # Split the string into a list
        name = name.split()
        
        # Get the first name
        first_name = name[0]

        # If the key doesn't exist, return 0
        count = nameDict.get(first_name, 0)

        # Add 1 to the count of names
        nameDict[first_name] = count   1

    return nameDict

CodePudding user response:

I agree that you were totally almost there. Here is a solution I came up with that maintains much of your original script, but mainly just changes the 'new_name' iteration bug into a list.append.

new_name_list = []

def name_counts(name_list):

nameDict = {}
for name in name_list:
    name = name.split()
    new_name_list.append(name[0])           # in the original code, 'new_name' was being reset 
                                            # with each iteration to that iteration's current value.                                         
                                            # Instead, there is now a list 'new_name_list' 
                                            # which is appended with each iteration.
for first_name in new_name_list:
    if first_name in nameDict:
        nameDict[first_name] =1       # Here the variable 'new_name' is being replaced. Instead of 
                                    # carrying the original logic, which would replace it with 'new_name_list',
                                    # it is replaced with the iterable, 'first_name', 
                                    # which seems to be the original intent of the given logic. 
    else:
        nameDict[first_name]=1
return nameDict

name_list = ["David Joyner", "David Zuber", "Brenton Joyner", "Brenton Zuber", "Nicol Barthel", "Shelba Barthel", "Shelba Crowley", "Shelba Fernald", "Shelba Odle", "Shelba Fry", "Maren Fry"] print(name_counts(name_list))

CodePudding user response:

Here is what I ultimately did:

def name_counts(name_list):
    nameDict = {}
    for name in name_list:
        name = name.split()
        new_name = name[0]

        if new_name in nameDict:
            nameDict[new_name]  = 1
        else:
            nameDict[new_name] = 1
    return nameDict
  • Related