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


Messages In This Thread
Manipulating List - by frenchyinspace - Oct-08-2019, 06:07 AM
RE: Manipulating List - by buran - Oct-08-2019, 07:19 AM
RE: Manipulating List - by perfringo - Oct-08-2019, 07:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Manipulating panda dataframes more python-like badtwistoffate 4 2,024 Jan-31-2023, 04:30 AM
Last Post: deanhystad
  Manipulating code to draw a tree Py_thon 8 3,232 Nov-21-2019, 05:00 PM
Last Post: sumana
  Manipulating __init__ method schniefen 5 3,510 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