python--几个重要内置函数(zip,fliter,map,sorted)
Posted jsit-dj-it
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python--几个重要内置函数(zip,fliter,map,sorted)相关的知识,希望对你有一定的参考价值。
# # zip 拉链方法 # l = [1,2,3] # l2 = [‘a‘,‘b‘,‘c‘] # l3 = (‘*‘,‘**‘,[1,2]) # d = ‘k1‘:1,‘k2‘:2,‘k3‘:3 # for i in zip(l,l2,l3,d): # print(i) # # # filter # def is_odd(x): # return x % 2 == 1 # # def is_str(s): # if type(s) != int: # return s and str(s).strip() # # ret = filter(is_odd, [1, 4, 6, 7, 9, 12, 17]) # # ret = filter(is_str, [1,‘hello‘, 6, 7,‘world‘,12,17]) # ret = filter(is_str, [1,‘hello‘,‘‘,‘ ‘,None,[],6,7,‘world‘,12,17]) # print(ret) # for i in ret: # print(i) # [i for i in[1, 4, 6, 7, 9, 12, 17] if i % 2 == 1] """ 用filter过滤出1~100中平方根是整数的数, """ # import math # def is_sqr(x): # return math.sqrt(x) % 1 ==0 # ret1 = filter(is_sqr,range(1,101)) # for i in ret1: # print(i) # map # ret = map(abs,[1,-4,6,-8]) # print(ret) # for i in ret: # print(i) # sorted # l = [1,-4,6,5,-10] # print(sorted(l)) # print(sorted(l,reverse=True)) # print(sorted(l,key=abs,reverse=True)) l = [‘ ‘,[1,2],‘hello world‘] new_1 = sorted(l,key=len) print(new_1)
以上是关于python--几个重要内置函数(zip,fliter,map,sorted)的主要内容,如果未能解决你的问题,请参考以下文章
python几个重要的函数(lambda,filter,reduce,map,zip)