• map
 #自定义map函数
def map_test(func, list):
res = []
for item in list:
res.append(func(item))
return res def add_one(x):
return x + 1 a = [1, 2, 3]
print(map_test(add_one, a))
print(map_test(lambda x:x + 1, a)) #终极版本 #python中的内置函数map(),功能同上
print('python的内置函数map()', list(map(lambda x:x + 1, a)))
#map(参数1:功能函数(命名函数或者是匿名函数,参数2:可迭代对象(列表,字符串...)))
#python2中map()函数处理的结果是一个列表,而python3中处理的结果是一个可迭代对象

map

  • filter
 #自定义filter函数
def filter_test(func, list):
res = []
for item in list:
if func(item):
res.append(item)
return res def bigger5(x):
if x > 5:
res = True
else:
res = False
return res a = [1, 2, 3, 10, 15, 6]
print(filter_test(bigger5, a)) #python内置函数filt(),功能同上
print(list(filter(bigger5, a)))
#filter(参数1:功能函数(命名函数或者是匿名函数,参数2:可迭代对象(列表,字符串...)))
#python2中filter()函数处理的结果是一个列表,而python3中处理的结果是一个可迭代对象

filter

  • reduce
 #自定义reduce函数
def reduce_test(func, list, init = None):
if init is None:
res = list.pop(0)
else:
res = init
for item in list:
res = func(res, item)
return res
a = [1, 2, 3]
print(reduce_test(lambda x, y:x + y, a)) #python内置函数reduce()
from functools import reduce
a = [1, 2, 3]
print(reduce(lambda x, y:x + y , a)) #
print(reduce(lambda x, y:x + y , a, 2)) #
#reduce(参数1:功能函数(命名函数或者是匿名函数),参数2:可迭代对象(列表,字符串...),参数3,初始值(可选参数))
#返回是一个值

reduce

①map():对可迭代对象中的每个元素都进行相同的处理,最后返回一个新的可迭代对象,python2中直接返回列表形式
②filter():对可迭代对象中的每个元素进行筛选,最后将符合条件的元素装进一个新的可迭代对象里,python2中直接返回列表形式
③reduce():对可迭代对象中的元素进行一个操作(如所有的值加起来),最后返回新数值
  • 其他内置函数
 # ==========abs==========绝对值
print(abs(-1)) # # ==========all==========可迭代对象的每个元素的布尔操作都是True时返回True,若传入的可迭代对象是空则返回True
print(all(['', 1, 's'])) # False # ==========any==========可迭代对象的每个元素的布尔操作中有一个是True时就返回True,若传入的可迭代对象是空则返回True
print(any(['', 5, ''])) # Ture # ==========bin==========将十进制数转成二进制数
print(bin(3)) # 0b11 # ==========hex==========将十进制数转成十六进制数
print(hex(10)) # 0xa # ==========oct==========将十进制数转成八进制数
print(oct(8)) # 0o10 # ==========bool==========布尔值操作: 空 0 None 布尔操作为False
print(bool([])) # False # ==========bytes==========编码 将传入的字符串用某种编码方式转换成二进制
print(bytes('陈', encoding='utf-8')) # b'\xe9\x99\x88'
print(bytes('陈', encoding='utf-8').decode('utf-8')) # 陈 # 译码 译码与编码应采用一样的 # ==========chr==========将ASCII码转成对应字符
print(chr(97)) # a # ==========ord=========将字符转成对应的ASCII码值
print(ord('a')) # # ==========dir==========打印某种对象里面的所有属性
print(dir(all)) # ==========divmod==========参数1除以参数2 返回一个元组(商,余数)
print(divmod(10, 3)) # (3, 1) # ==========eval==========
dic = {'name': 'chen'}
dic_str = str(dic)
# ===================================功能1:提取成传入的字符串中的数据类型
print(dic_str) # '{'name': 'chen'}'
print(type(dic_str)) # <class 'str'>
print(eval(dic_str)) # {'name': 'chen'}
print(type(eval(dic_str))) # <class 'dict'>
# ===================================功能2:计算字符串中的数学运算
s = '1 + 2 * 1'
print(eval(s)) # # ==========hash==========hash运算
print(hash('chen')) # -1076846465005340399
print(hash('chenyuanyuan')) # -8978753568388561982 #hash()---->hash运算 # ==========help==========函数功能解释
print(help(all)) # ==========isinstance==========判断某个对象是否是某种数据类型的实例对象
print(isinstance('', str)) # True # ==========globals、locals==========全局变量、局部变量
name = 'chen' def test():
age = 18
print(globals()) # 打印所有的全局变量,包括name
print(locals()) # {'age': 18} #打印所有的局部变量 test() # ==========min、max==========最小值、最大值
dic = {'chen': 100, 'wang': 200, 'li': 500, 'do': 600}
# ============一个可迭代参数
print(min(['a01', 'b10', 'c1'])) # a01 遍历出每个元素,比较它们的的第一位的ASCII码值,找到最小后就停止比较
# print(min(['a01', 'b10', 'c1', 30])) #报错 不同数据类型无法比较
print(min(dic)) # chen
print(max(dic)) # wang
print(min(dic.values())) #
print(max(dic.values())) #
print(max(zip(dic.values(), dic.keys()))) # (600, 'do') #max与zip结合找出value值最大的那一组
# ============两个参数
print(min(dic, key=lambda k: dic[k])) # chen
print(max(dic, key=lambda k: dic[k])) # do # ==========zip==========拉链操作,将多个可迭代对象(列表,元组,字符串)对应元素组成一个元组,多余元素自动去掉,返回可迭代对象
print(list(zip('abc', 'def', 'ghihhhhh'))) # [('a', 'd', 'g'), ('b', 'e', 'h'), ('c', 'f', 'i')]
print(list(zip(dic.keys(), dic.values()))) # [('chen', 100), ('wang', 200), ('li', 500), ('do', 600)] # ==========pow==========参数1的参数2次方,结果对参数3进行取余操作
print(pow(2, 3)) #
print(pow(2, 3, 1)) # # ==========reversed==========反转可迭代对象中元素的排列顺序,返回迭代器
a = [1, 2, 3]
print(list(reversed(a))) # [3, 2, 1] # ==========round==========四舍五入取整
print(round(2.66)) # # ==========set==========将其他数据类型转换成集合
print(set('hello')) # {'h', 'o', 'e', 'l'} # ==========slice==========切片:参数1-->起始索引,参数2-->结束索引,参数3-->步长
a = 'ehllovihgh'
s1 = slice(2, 6, 2)
print(s1.start) #
print(s1.stop) #
print(s1.step) #
print(a[s1]) # lo # ==========sorted==========排序
a = [1, 25, 2, 3]
print(sorted(a)) #[1, 2, 3, 25] #不同数据类型任然无法进行排序比较
lists = [{'name': 'chen', 'age': 18}, {'name': 'wang', 'age': 25}, {'name': 'li', 'age': 9}]
print(sorted(lists, key = lambda dic:dic['age']))
#[{'name': 'li', 'age': 9}, {'name': 'chen', 'age': 18}, {'name': 'wang', 'age': 25}]
info = {'liu': 100, 'li': 12200, 'chen': 100000}
print(sorted(info.keys())) #['chen', 'li', 'liu'] #比较key值
print(sorted(info.values())) #[100, 12200, 100000] #比较value值
print(sorted(info, key = lambda key: info[key])) #['liu', 'li', 'chen'] #比较value值,返回key值
print(sorted(zip(info.values(), info.keys()))) #[(100, 'liu'), (12200, 'li'), (100000, 'chen')] # ==========type==========查看数据类型
print(type('ll')) # <class 'str'> # ==========sum==========求和
print(sum([5, 2, 3])) # # ==========range==========范围
for item in range(5):
print(item)
#
#
#
#
#
for item in range(5, 7):
print(item)
#
# # ==========__import__==========引入字符串类型的模块名
m = __import__('...')
m.函数名()

其他内置函数

最新文章

  1. mysql order by 优化 |order by 索引的应用
  2. python入门练习题3(函数)
  3. 写给java开发的运维笔记
  4. 一起来学习Android自定义控件1
  5. 【CodeForces 697C】Lorenzo Von Matterhorn(LCA)
  6. 如何编译Apache Hadoop2.6.0源代码
  7. xcode 产生指定颜色的图片imageWithColor
  8. (转载)高速ADC的关键指标:量化误差、offset/gain error、DNL、INL、ENOB、分辨率、RMS、SFDR、THD、SINAD、dBFS、TWO-TONE IMD
  9. WP开发笔记——字符串 转 MD5 加密
  10. 2013 Multi-University Training Contest 1 3-idiots
  11. Linux SocketCan client server demo hacking
  12. Java 单链表的倒置
  13. 三级联动数据表db_nove.sql
  14. URAL 1260 Nudnik Photographer DFS DP
  15. tornado settings想到的
  16. Eclipse详细设置护眼背景色和字体颜色并导出
  17. zabbix误报交换机重启
  18. jQuery_parent() parents() closest()区别
  19. 注册Docker官网账号 注册按钮不能点
  20. BZOJ3505 CQOI2014数三角形(组合数学)

热门文章

  1. Raspberry Pi 开机启动QT程序
  2. 8、服务发现&amp;服务消费者Feign
  3. malloc&amp;&amp;fread
  4. ssh-keyscan - 收集 ssh 公钥
  5. oracle hint 强制索引(转)
  6. 11、for和range的用法
  7. TFS&mdash;&mdash;更改计算机名称,影响TFS使用
  8. http://elasticsearch-py.readthedocs.io/en/master/api.html
  9. CSS:CSS 图像拼合技术
  10. 14、java实现poi操作excel,包括读和写日期格式,并且设置字体样式