zip函数
list(zip(('a','b','c'),(1,2,3)))
[('a', 1), ('b', 2), ('c', 3)]
将2个不同的序列一一对应变成一个列表
max函数
- max()可以比较可用迭代器遍历的所有类型,从每个元素的第一个位置开始比较
- max() 如果比较的是字符串,则按照ASCII码值的顺序比较
- max()若同时比较字符串和数字—异常:类型不统一
1.使用max比较字典
dictionary = {'num1':1,'num2':2,'num3':3,'num4':4}
max(dictionary) #只显示key
print(max(zip(dictionary.keys(),dictionary.values())))
num4
('num4', 4)
使用max比较字典时,只会根据字典的key来比较,并且只会显示最大的key,如果配和使用zip,则会显示键值对
2.使用max比较多个字典
people = [{'name':'heley','age':100},{'name':'smith','age':80}]
max(people,key=lambda dic:dic['age'])
{'name': 'heley', 'age': 100}
比较多个字典,传入方法
min的使用方式与max类似
sorted的使用方式与max类似
print(sorted(people,key=lambda dic:dic['age']))
print(sorted(zip(dictionary.values(),dictionary.keys())))
[{'name': 'smith', 'age': 80}, {'name': 'heley', 'age': 100}]
[(1, 'num1'), (2, 'num2'), (3, 'num3'), (4, 'num4')]
类型转换内置函数
-
list 转化为列表
-
str 转化为字符串
-
tunple 转化为元组
-
eval
- 将字符串转换为元组,列表,字典
可以把list,tuple,dict和string相互转化。 ################################################# 字符串转换成列表 >>>a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]" >>>type(a) <type 'str'> >>> b = eval(a) >>> print b [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]] >>> type(b) <type 'list'> ################################################# 字符串转换成字典 >>> a = "{1: 'a', 2: 'b'}" >>> type(a) <type 'str'> >>> b = eval(a) >>> print b {1: 'a', 2: 'b'} >>> type(b) <type 'dict'> ################################################# 字符串转换成元组 >>> a = "([1,2], [3,4], [5,6], [7,8], (9,0))" >>> type(a) <type 'str'> >>> b = eval(a) >>> print b ([1, 2], [3, 4], [5, 6], [7, 8], (9, 0)) >>> type(b) <type 'tuple'>
- 执行表达式
>>>x = 7 >>> eval( '3 * x' ) 21 >>> eval('pow(2,2)') 4 >>> eval('2 + 2') 4 >>> n=81 >>> eval("n + 4") 85
-
set 转化为不重复序列
-
int 字符串转化为int类型
其他内置函数
-
reversed 反转
-
range 返回整数集合
-
slice 提前定义切片
text = 'hello' s1 = slice(3,5) s2 = slice(1,5,2) #2为步长 print(text[s1]) #lo print(text[s2]) #el s2.start # 1 s2.stop # 5 s2.stop # 2
-
type 判断数据类型
-
vars
- 无参数 vars() 表示locals() 显示所有局部变量
- 有参数 vars(dict) 打印一个对象的所有方法,以字典形式返回
-
-import-(‘test’) 导入类包,()中是字符串类型,用于动态导入类 (-是下划线)
数学计算内置函数
- round(3.5) 四舍五入
- sum() 求和
- pow(a,b) 计算a的b次方
- pow(a,b,c) 计算a的b次方对c取余
相对的函数
1.chr / ord
- chr 打印该数字在ASCII码表中的值
- ord 打印该元素在ASCII码表中的位置数字