Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Manipulating List
#1
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,
Reply
#2
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Manipulating panda dataframes more python-like badtwistoffate 4 2,006 Jan-31-2023, 04:30 AM
Last Post: deanhystad
  Manipulating code to draw a tree Py_thon 8 3,204 Nov-21-2019, 05:00 PM
Last Post: sumana
  Manipulating __init__ method schniefen 5 3,489 May-06-2019, 11:22 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020