首页 > 编程语言 > 详细

python--几个重要内置函数(zip,fliter,map,sorted)

时间:2019-08-30 23:27:43      阅读:110      评论:0      收藏:0      [点我收藏+]
# # 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)

原文:https://www.cnblogs.com/jsit-dj-it/p/11437290.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!