我正在使用Python进行DS课程,他们要求我修复下一个功能。由于我正在学习Python并行编程以获取此类,所以我有点失去任何帮助。将不胜感激!
split_title_and_name
people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']
def split_title_and_name(person):
return person.split()[0] + ' ' + person.split()[-1]
#option 1
for person in people:
print(split_title_and_name(person) == (lambda person:???))
#option 2
#list(map(split_title_and_name, people)) == list(map(???))
答案 0 :(得分:1)
根据功能的名称,我想你想要这个:
>>> people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']
>>> def split_title_and_name(people_list):
... return [p.split('. ') for p in people_list]
... # ^ Assuming title will always be followed by dot '.',
# There will be only one '.' dot in the sample string
>>> split_title_and_name(people)
[['Dr', 'Christopher Brooks'],
# ^ ^
# Title Name
['Dr', 'Kevyn Collins-Thompson'],
['Dr', 'VG Vinod Vydiswaran'],
['Dr', 'Daniel Romero']]
注意:而且你绝对不需要 lambda 。在任何情况下都不需要它。
答案 1 :(得分:0)
list(map(map(title_and_name,people))== list(map(lambda person:person.split()[0] +''+ person.split()[-1],people))
答案 2 :(得分:0)
此项工作(:
people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']
def split_title_and_name(person):
title = person.split()[0]
lastname = person.split()[-1]
return '{} {}'.format(title, lastname)
list(map(split_title_and_name, people))