list1 = ['1 ', '2 ', ' 3 '] # map: 对列表中的每个元素做相同的操作 # "".strip()去空格 list1 = list(map(str.strip, list1)) print(type(list1)) result = "".join(list1) print(result) list1 = ['1 --', '2 --', '-- 3 '] # map: 对列表中的每个元素做相同的操作 # "".strip()去空格 list1 = list(map(lambda x:x.strip(" --"), list1)) print(type(list1)) result = "".join(list1) print(result) # 对列表中的每个元素两两相乘求结果 # reduce: 对列表的多个元素执行的相同操作并把结果合并 from functools import reduce list2 = [1, 2, 3, 4, 5, 6, 7, 8] # result = 1 # for x in list2: # result *= x # print(result) result = reduce(lambda x,y: x*y, list2) print(result) list_a = [12, 213, 22, 2, 2, 2, 22, 2, 2, 32] result = list(map(lambda x:x[1], filter(lambda item:item[0]%2 == 1, enumerate(list_a)))) print(result) import jieba name = '一把把把把住了' print(list(jieba.cut(name))) a = {i: i for i in range(0, 10)}.get(0, 10) result = reduce(lambda x, y: x+y, map(lambda x, y:x**y, range(4), range(10))) print('========', result)
map, reduce, jieba
最新推荐文章于 2020-11-28 06:01:35 发布