Answer to Question #6739 in Python for ALAN TING

Question #6739
Write a function lastfirst() that takes a list of strings as a parameter. Each string in the list has the format 'Last, First' where Last is a last name and First is a first name. The function lastfirst() returns a list containing two sublists. The first sublist is a list of all the first names and the second sublist is a list of all the last names. The following shows how the function would be called on an example parameter:

>>> lastfirst(['Gerber, Len', 'Fox, Kate', 'Dunn, Bob']
[['Len', 'Kate', 'Bob'], ['Gerber', 'Fox', 'Dunn']]
1
Expert's answer
2012-02-23T09:32:52-0500
#########################
# code of the function

#########################
def lastfirst(a):
# list of last
names
lastNames=[]
# list of first names
firstNames=[]
for x in
a:
n=x.find(',') # look for the comma
lastNames.append(x[0:n]) # extract
all symbols before ','
firstNames.append(x[n+2:]) # extract all symbols after
', '
return [firstNames, lastNames] # print the
result


#########################
# test
command
#########################
print lastfirst(['Gerber, Len', 'Fox,
Kate', 'Dunn, Bob'])

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS