Python Forum

Full Version: Manipulating List
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm midway through my first semester of computer science. I've been staying as far ahead of the homework as possible because I find it fun and challenging. But this problem has stumped me like no other. I do not have much experience with lists and manipulating them and I suspect I've made this a lot harder for myself than I needed to. Couldn't find any info on how to do this online and I need some guidance. Thanks in advance. Assignment below:

Write a program that prompts the user to enter a list of names. Each person's name is separated from the next by a semi-colon and a space ('; ') and the names are entered lastName, firstName (i.e. separated by ', '). Your program should then print out the names, one per line, with the first initial of the first name, followed by ".", and followed by the last name.

A sample run of your program should look like:

Please enter your list of names: Cohn, Mildred; Dolciani, Mary P.; Rees, Mina; Teitelbaum, Ruth; Yalow, Rosalyn

You entered:

M. Cohn
M. Dolciani
M. Rees
R. Teitelbaum
R. Yalow

Thank you for using my name organizer!

Here's what I have so far:

nlist= input("Enter list of names like this: Bastien, Sacha; Smith, Adam")
name1=nlist.split("; ")                                                    #splits the list into sublists of [lastname, firstname]
name2=[item.split(",") for item in name1]                                 


for item in range(len(name1)):
    name2= [item.split(",") for item in name1]
    name3=name2[-1::-1]
    name4=" ".join(name3)
    print(name4[0]+". "+name2[0])
I've run into many problems here.
1. having split the original list into sublists, how to further split the names into first and last name.
Originally I had name2=name1[0].split"," but that didn't work for looping as the index had to be specified.
2. I was able to reverse the order of each full name using name3=name2[-1::-1], but that no longer works.
3. Need to join them back together, this also doesn't work

Thanks,
(Oct-08-2019, 06:07 AM)frenchyinspace Wrote: [ -> ]1. having split the original list into sublists, how to further split the names into first and last name.

your script already does this. maybe more meaningful variable names would help to see it:
user_input = input("Enter list of names like this: Bastien, Sacha; Smith, Adam: ")
persons = user_input.split("; ")
print(persons)                                                    
names =  [person.split(", ") for person in persons] # splits the list into sublists of [lastname, firstname]
print(names)
Output:
Enter list of names like this: Bastien, Sacha; Smith, Adam: Cohn, Mildred; Dolciani, Mary P.; Rees, Mina; Teitelbaum, Ruth; Yalow, Rosalyn ['Cohn, Mildred', 'Dolciani, Mary P.', 'Rees, Mina', 'Teitelbaum, Ruth', 'Yalow, Rosalyn'] [['Cohn', ' Mildred'], ['Dolciani', ' Mary P.'], ['Rees', ' Mina'], ['Teitelbaum', ' Ruth'], ['Yalow', ' Rosalyn']]
As you can see names is already list of lists in the form [[last_name1, first_name1], [last_name2, first_name2]...]
Just be careful when split on ; and , , i.e. it may be better to split at ; or , and later on strip the extra space from the resulting string.

What you need to do is to just iterate over it and retrieve each par of first and last name
for last_name, first_name in names:
    # now here do something to get the desired otput
Just for oneliner fun:

>>> names = 'Cohn, Mildred; Dolciani, Mary P.; Rees, Mina; Teitelbaum, Ruth; Yalow, Rosalyn'
>>> print(*[f'{item[1][0]}. {item[0]}' for item in [name.split(', ') for name in names.split('; ')]], sep='\n')
M. Cohn
M. Dolciani
M. Rees
R. Teitelbaum
R. Yalow
EDIT: to get expected output ('You entered:' followed by empty line):

>>> print('You entered:\n', *[f'{item[1][0]}. {item[0]}' for item in [name.split(', ') for name in names.split('; ')]], sep='\n')
You entered:

M. Cohn
M. Dolciani
M. Rees
R. Teitelbaum
R. Yalow