''' 练习二: 使用map()和reduce()函数实现 自己实现一个函数,类似于upper()或lower()函数的功能 例如: string = 'ABCDEFG' 转换之后 string = 'abcdefg ''' # list_a = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] # list_b = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] # for x in range(0,256): # print('%s--%s'%(x,chr(x))) # 65-90 # 97-122 # rs = input('请输入您要转换的字符') # list1 = [] # for x in rs: # a = ord(x) # if 65 <= a <= 90: # a = a + 32 # elif 97 <= a <= 122: # a = a - 32 # b = chr(a) # list1.append(b) # print(list1) # c = ''.join(list1) # print(c) # ABCDEFG 转换为abcdefg ''' 1.把ABCDEFG每一个字符转换为小写。 ''' def LOWER(string): # char_lower函数是作用到map()函数的。 def char_lower(s): # 将26个英文字母对应的小写字母,在字典中进行定义。 chars_dict = {'A': 'a', 'B': 'b', 'C': 'c', 'D': 'd', 'E': 'e', 'F': 'f', 'G': 'g', 'H': 'h', 'I': 'i', 'J': 'j', 'K': 'k', 'L': 'l', 'M': 'm', 'N': 'n', 'O': 'o', 'P': 'p', 'Q': 'q', 'R': 'r', 'S': 's', 'T': 't', 'U': 'u', 'V': 'v', 'W': 'w', 'X': 'x', 'Y': 'y', 'Z': 'z'} # 根据s取值,如果取不到,就赋原值 char = chars_dict.get(s, s) return char rs = list(map(char_lower, string)) rs = reduce(lambda x,y:x+y, rs) return rs res = LOWER('AbCdEF') print(res) print(type(res))