map()函数的用法

str1 = "This is a cat"
mapper = map(str.upper, str1)
lst = list(mapper)  # ['T', 'H', 'I', 'S', ' ', 'I', 'S', ' ', 'A', ' ', 'C', 'A', 'T']
# lst = ''.join(lst)  # THIS IS A CAT
print(lst)


def to_upper(s):
    return s.upper()
def my_join(s):
    return ''.join(s)
new_str = list(map(to_upper, str1))
print(my_join(new_str))  # THIS IS A CAT


str2 = "How are you today?"
mystr2 = list(map(lambda x:x.upper(),str2))
print(my_join(mystr2))   # HOW ARE YOU TODAY?

  

posted @ 2023-05-19 13:40  sangern  阅读(39)  评论(0)    收藏  举报