32 posts were merged into an existing topic: How to select only last names?
In the 1st exercise, I think that you should allow bellow to work, because you actually want to remove the space between authors
author_names = authors.split(", ")
I agree!
That’s exactly what I did, and was surprised when it was not accepted as right.
I tested with print and it seemed perfect.
I struggled to figure out what the expected goal was.
Uhm. Have you looked at the string though? What delimits the authors? It’s not ", "
authors = "Audre Lorde,Gabriela Mistral,Jean Toomer,An Qi,Walt Whitman,Shel Silverstein,Carmen Boullosa,Kamala Suraiyya,Langston Hughes,Adrienne Rich,Nikki Giovanni"
I don’t know how and why, but I checked again and my saved solution looks like this:
authors = "Audre Lorde, William Carlos Williams, Gabriela Mistral, Jean Toomer, An Qi, Walt Whitman, Shel Silverstein, Carmen Boullosa, Kamala Suraiyya, Langston Hughes, Adrienne Rich, Nikki Giovanni"
author_names = authors.split(’,’)
print(author_names)
def grab_last_name(name_list):
** last_name_list = **
** for name in author_names:**
** last_name_list.append(name.split()[-1])**
** return last_name_list**
author_last_names = grab_last_name(author_names)
print(author_last_names)
So, ", " delimits the authors.
Interesting thing is that now when I reset this exercise I get a list of authors delimited only by “,” as you posted.
But then, I tried to paste the first part of my code (including authors list whit blank space) after reset and I still get:
“Value for author_names did not match the expected value.”
List looks perfect:
[‘Audre Lorde’, ‘William Carlos Williams’, ‘Gabriela Mistral’, ‘Jean Toomer’, ‘An Qi’, ‘Walt Whitman’, ‘Shel Silverstein’, ‘Carmen Boullosa’, ‘Kamala Suraiyya’, ‘Langston Hughes’, ‘Adrienne Rich’, ‘Nikki Giovanni’]
Then I remove blank space after “,” and my code is accepted as correct eventhough list looks wrong (whit blank space before names in the list:
[‘Audre Lorde’, ’ William Carlos Williams’, ’ Gabriela Mistral’, ’ Jean Toomer’, ’ An Qi’, ’ Walt Whitman’, ’ Shel Silverstein’, ’ Carmen Boullosa’, ’ Kamala Suraiyya’, ’ Langston Hughes’, ’ Adrienne Rich’, ’ Nikki Giovanni’]
Anyway, I’m quite sure that the list for some reason looked different, and [brunpierreantoine] had the same problem.
I’ve found another discussion on the same problem:
Hi,
The code below printed a list of surnames as was required, but the code editor did not accept the solution. This is what was printed out:
[‘Lorde’, ‘Williams’, ‘Mistral’, ‘Toomer’, ‘Qi’, ‘Whitman’, ‘Silverstein’, ‘Boullosa’, ‘Suraiyya’, ‘Hughes’, ‘Rich’, ‘Giovanni’]
#And this was the code:
def author_last_names():
surnames = []
for index in range(0, len(author_names)):
split = author_names[index].split()
surnames.append(split[-1])
print(surnames)
author_last_names()
What have I done wrong?
Double check the requirements. Does it ask you to print values out or does it mention something else?
Hi,
If the string contains only the delimiter as its content, like seen below, why does ‘split’ return two elements list?
test = “*”
print(test.split(’*’))
Result: [’’, ‘’]
Because its counterpart, str.join()
needs that pattern in order to reconstruct the string.
>>> test = "*"
>>> test_list = test.split('*')
>>> test_list
['', '']
>>> test_str = '*'.join(test_list)
>>> test_str
'*'
>>>
Now let’s tweak the list and remove the quotes…
>>> '*'.join([''])
''
>>> '*'.join([])
''
>>>