码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-08-30 22:47:33      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:方法   strip()   code   return   col   ted   imp   int   turn   

# # 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)

标签:方法   strip()   code   return   col   ted   imp   int   turn   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!