(100-01)常用函数解析
* eval()
在字符串中能计算
>>> "5+5"
Out[51]: '5+5'
>>> eval("5+5")
Out[52]: 10
>>> "'hello'+'world'"
Out[53]: "'hello'+'world'"
>>> eval("'hello'+'world'")
Out[54]: 'helloworld'
* print()
>>> name="John"
>>> print(name)
John
基本输入出
>>> print("His name is %s"%name)
His name is John
>>> age=26
>>> print("His name is %s, age is %d"%(name,age))
His name is John, age is 26
采用占位符输出
>>> a=3.1415926
>>> print ("%d"%a)
3
取整数
>>> print ("%f"%a)
3.141593
输出浮点数
>>> print ("%.2f"%a)
3.14
保留2位小数输出
>>> print ("%.9f"%a)
3.141592600
保留9位小数,不够用0填充
>>> b=98759.1415926
>>> print("%4d"%b)
98759
保留4位整数
>>> print("%06d"%b)
098759
保留6位整数,不够填充0
>>> import math
>>> print ("PI=%f"%math.pi)
PI=3.141593
>>> print ("PI=%10.3f"%math.pi)
PI= 3.142
总长为10位,小数位保留3位
>>> print ("PI=%-10.3f"%math.pi)
PI=3.142
总长为10位,但要左对齐,
>>> print ("PI=%06d"%math.pi)
PI=000003
总长为6位,,只保留整数没6位的用0补上
数字占位符基本用法
--------------------
>>> greet="Give my regards for you"
>>> print("%.3s"%greet)
Giv
>>> print("%.*s"%(3,greet))
Giv
取字符串取3个字符
>>> print("%25s"%greet)
Give my regards for you
至少显示25个字符,不够左边用空格填充
>>> print("%7.3s"%greet)
Giv
取3个字符,用7个字符的位置展示
>>> print("%-7.3s"%greet)
Giv
取3个字符,用7个字符的位置展示,左对齐
>>> import datetime
>>> today=datetime.date.today()
>>> today
Out[44]: datetime.date(2016, 3, 2)
>>> str(today)
Out[45]: '2016-03-02'
>>> repr(today)
Out[46]: 'datetime.date(2016, 3, 2)'
%r-repr() %s-str() 区别 repr只转为字符输出来
字符占位符基本用法
-------------------------
>>> print("age is: "+`age`)
age is: 26
>>> print("age is: "+str(age))
age is: 26
字符串和其它数据类型连接输出
>>> carinfo={'color':"red","brand":"toyota","width":"2m"}
>>> print ("brand of my car is %(brand)s"%carinfo)
brand of my car is toyota
高级一点输出
print 后面会自动加 "\n"
* format()
>>> print("brand of the car is {0},color is {1}".format("toyota","red"))
brand of the car is toyota,color is red
按索引引用
>>> print ("website is {website}".format(website="www.baidu.com"))
website is www.baidu.com
按键值引用
>>> position ="First={0[0]}, Third={0[2]}"
>>> position.format("best")
Out[12]: 'First=b, Third=s'
序列偏移量,但在format中,序列不能用负数做为偏移量的值
>>> mydict ={"color":"red","name":"car"}
>>> template = "this is {0[name]}"
>>> template.format(mydict)
Out[15]: 'this is car'
字典直接用 键引用
>>> import sys,math
>>> 'PI is {0.pi}.My lptop runs {1.platform}'.format(math,sys)
Out[17]: 'PI is 3.14159265359.My lptop runs linux2'
添加属性
>>> "{0:X},{1:o},{2:b}".format(100,100,100)
Out[18]: '64,144,1100100'
进制的显示
X:十六进制
o: 八进制
b: 二进制
* zip()
>>> str1="hello"
>>> str2="world"
>>> zip(str1,str2)
Out[5]: [('h', 'w'), ('e', 'o'), ('l', 'r'), ('l', 'l'), ('o', 'd')]
分别取两个序列对应值组成新的元组列表
>>> str1="he"
>>> zip(str1,str2)
Out[7]: [('h', 'w'), ('e', 'o')]
可以看到是以参数中最少的序列的元素为基准
>>> mydict1={"name":"John"}
>>> mydict2={"sex":"male"}
>>> zip(mydict1,mydict2)
Out[12]: [('name', 'sex')]
字典只取键操作
>>> str1="hello"
>>> zip(str1)
Out[15]: [('h',), ('e',), ('l',), ('l',), ('o',)]
单个参数,元组会少一个数
>>> mylist1=[1,3,5,7,9]
>>> mylist2=[2,4,6,8,10]
>>> ziplist = zip(mylist1,mylist2)
>>> ziplist
Out[24]: [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
>>> zip(*ziplist)
Out[25]: [(1, 3, 5, 7, 9), (2, 4, 6, 8, 10)]
列表压缩和解压缩
>>> mydict
Out[28]: {'color': 'red', 'size': 3}
>>> dict(zip(mydict.values(),mydict.keys()))
Out[29]: {3: 'size', 'red': 'color'}
字典键值互换
* range()
>>> range(1,10)
Out[19]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
返回指定范围的列表
>>> range(1,10,2)
Out[20]: [1, 3, 5, 7, 9]
第三个参数指定步数,这里为2
* enumerate()
专用于列表同时提取键值
>>> mylist=["name",88]
>>> list(enumerate(mylist))
Out[37]: [(0, 'name'), (1, 88)]
* map()
>>> mylist
Out[47]: [1, 2, 3]
>>> map(lambda x:x+3,mylist)
Out[46]: [4, 5, 6]
取列表中的每个数去加3
>>> [x+3 for x in mylist ]
Out[48]: [4, 5, 6]
列表解析也可以做到
>>> list1
Out[52]: [1, 2, 3, 4, 5]
>>> list2
Out[53]: [5, 6, 7, 8, 9]
>>> list3
Out[54]: [10, 11, 12, 13, 14]
>>> map(lambda x,y,z:z+y+z,list1,list2,list3)
Out[55]: [25, 28, 31, 34, 37]
分别取列表的元素去相加 ,元素的数目要一致
这样用列表解析和循环就难做到了
* reduce()
>>> list1=range(1,101)
>>> reduce(lambda x,y:x+y,list1)
Out[71]: 5050
1+..+100 的值 reduce 是依次取值
map是上下运算 reduce 是横着逐个运算
sum(list1)
Out[74]: 5050
>>> sum=0
>>> for i in list1:
... sum+=i
...
>>> sum
Out[74]: 5050
循环是可以达到,但复杂
求两个列表对应元素相乘之后的和
分析:相乘是上下操作,和是横向操作
>>> list1
Out[82]: [1, 2, 3, 4, 5]
>>> list2
Out[83]: [5, 6, 7, 8, 9]
>>> reduce(lambda x,y:x+y,map(lambda x,y:x*y,list1,list2))
Out[85]: 115
>>> reduce(lambda sum,(x,y):sum+x*y,zip(list1,list2),0)
Out[91]: 115
>>> from operator import add,mul
>>> reduce(add,map(mul,list1,list2))
Out[93]: 115
轮子的力量很大
* filter()
>>> list1
Out[95]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> filter(lambda x:x%2==0,list1)
Out[96]: [2, 4, 6, 8, 10]
>>> [x for x in list1 if x%2==0]
Out[97]: [2, 4, 6, 8, 10]
过滤出偶数