1.map()用法:
people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero'] def split_title_and_name(person): person_split = person.split() return person_split[0]+person_split[2] list(map(split_title_and_name, people)) """ output: ['Dr.Brooks', 'Dr.Collins-Thompson', 'Dr.Vinod', 'Dr.Romero'] """
2.关于lambda函数的用法
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 x: x.split()[0] + ' ' + x.split()[-1])(person)) #option 2 list(map(split_title_and_name, people)) == list(map(lambda person: person.split()[0] + ' ' + person.split()[-1], people))
3.关于list的用法
def times_tables(): lst = [] for i in range(10): for j in range (10): lst.append(i*j) return lst times_tables() == [j*i for i in range(10) for j in range(10)]
lowercase = 'abcdefghijklmnopqrstuvwxyz' digits = '0123456789' correct_answer = [a+b+c+d for a in lowercase for b in lowercase for c in digits for d in digits] correct_answer[:50]