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)

 

posted @ 2019-08-30 21:48  DingJie1024  阅读(172)  评论(0编辑  收藏  举报