14-python--inner

2021-01-30 11:13

阅读:488

标签:对象   创建   简单   repr   tool   too   zip   推导   求和   

# python提供了68个内置函数。
# eval:剥去字符串的外衣运算里面的代码
# exec:与eval几乎一样,处理代码流
# hash:获取一个对象的hash值
# help:获取对象的详细说明
# callable:是否可以调用

# lambda:也叫一句话函数,比较简单。
# def func(a, b):
# return a + b
# func1 = lambda a, b: a + b
# ret = func1(1, 2)
# print(ret)
# s = ‘asdfhqwe‘
# func2 = lambda s:(s[0], s[2])
# print(func2(s))
# func3 = lambda a, b: a if a > b else b
# print(func3(0, 10))

# int
# float
# complex(1,2) # 1+2j
# bin\oct\hex
# divmod(10,3) # (3,1)
# round(1.123123109865,2) # 1.12
# pow(2,3) # 8
# pow(2, 3, 2) # 2
# bytes:用于不同编码之间的转化
# ord:输入自负找该自负编码的位置
# chr:输入位置数字找出对应的字符
# repr:返回一个对象的string形式
# s1 = ‘十大‘
# print(s1)
# print(repr(s1))
# msg = ‘%r‘ %(s1)
# print(msg)
# all:可迭代对象中全部都是true才是true
# any:可迭代对象中有一个是true就是true

# print
# print(1,2,3,4, sep=‘+‘, end=‘\t‘, file=None)
# print(1,2,3,4, sep=‘+‘, end=‘\t‘, file=None)

# dict 创建字典的集中方式
# 直接创建
# 元祖的解构
# dic1 = dict([(1, ‘one‘), (2, ‘two‘), (3, ‘three‘)])
# dic2 = dict(one=1, two=2)
# print(dic1)
# print(dic2)
# fromkeys
# update
# 字典推导式
# abs:绝对值
# sum:求和

# max:最大值
# print(max(2,3,4,5,6,10))
# reversed:序列翻转,
# zip:拉链方法
# l1 = [1, 2, 3, 4, 5]
# tu1 = (‘one‘, ‘two‘, ‘three‘, ‘four‘)
# s1 = ‘asd‘
# obj = zip(l1, tu1, s1)
# print(obj)
# for i in obj:
# print(i)
# sorted:排序

# map:
# min:最小值,凡事可以加key的,他会自动将可迭代对象中的每个元素按照顺序传入key对应的函数中,以返回值比较
# l1 = [1, 3, -4, -9]
# def abss(a): return abs(a)
# print(min(l1,key=abss))
# dic = {‘a‘: 3, ‘b‘: 2, ‘c‘: 5}
# def func(a):
# return dic[a]
# print(min(dic,key=func))
# print(min(dic, key=lambda a:dic[a]))

# l2 = [(‘s‘, 16), (‘f‘, 19), (‘t‘, 5), (‘o‘, 33)]
# print(min(l2, key=lambda tu1: tu1[1]))

# sorted
# l1 = [1,4,6,3,2]
# l2 = sorted(l1)
# print(l2)
# l2 = [(‘s‘, 16), (‘f‘, 19), (‘t‘, 5), (‘o‘, 33)]
# print(sorted(l2, key=lambda tu2: tu2[1], reverse=True)) # 返回的是一个列表

# filter:列表推导式的筛选模式
# l1 = [1,5,7,0,3,4]
# ret = filter(lambda x: x > 3, l1)
# print(list(ret))

# l1 = [1, 4, 9, 16, 25]
# print([i**2 for i in range(6)])
# ret = map(lambda x: x**2, range(6))
# print(ret)
# print(list(ret))

# reduce
# from functools import reduce
# def func(x,y):
# ‘‘‘
# 第一次:x y : 11 2 x + y = 记录: 13
# 第二次:x = 13 y = 3 x + y = 记录: 16
# 第三次 x = 16 y = 4 .......
# ‘‘‘
# return x + y
#
# l = reduce(func,[11,2,3,4])
# print(l)

14-python--inner

标签:对象   创建   简单   repr   tool   too   zip   推导   求和   

原文地址:https://www.cnblogs.com/Daspig/p/12820692.html


评论


亲,登录后才可以留言!