Python知识点汇总
发布时间
阅读量:
阅读量
字符串
str的split方法
str.split(sep=None, maxsplit=-1)
sep分割符,maxsplit最大分割次数
015字符串:格式化
print("{0} love {1}.{2}".format("I","FishC","com"))
print("{a} love {b}.{c}".format(a="I",b="FishC",c="com"))
"{{0}}".format("不打印")
'{0:.1f}{1}'.format(27.658,'GB')
'%c' % 97
'%c %c %c' % (97,98,99)
'%s' % 'I love FishC'
'%d + %d = %d' % (4,5,4+5)
'%x' % 10 #十六进制
代码解读
016 序列
list()
tuple()
len() # 长度
max()
min()
sum()
sorted(...)
reversed(...) # 返回迭代器对象
enumerate(...) # 返回(索引,值)构成的元组 的迭代器对象
a=[1,2,3,4,5]
b=[4,5,6]
list(zip(a,b))
代码解读
017 函数
函数文档
函数名.doc
help(函数名)
关键字参数
直接指定哪个参数=什么,不用管参数顺序
默认参数
形参
收集参数
未知参数长度
返回值
所有函数都有返回值,没有return的返回None
全局变量函数内部均可访问,但不能修改,若要修改,修改前声明为global变量,类似static
内嵌函数和闭包
闭包返回值类似于函数指针
def FunX(x):
def FunY(y):
return x*y
return FunY
代码解读
i=FunX(8)
print(i(5))
print(FunX(8)(5))
代码解读
方法一:
def Fun1():
x=[5] # 必须用列表,存放在堆里;或用nonlocal关键字
def Fun2():
x[0]*=x[0]
return x[0]
return Fun2()
print(Fun1())
代码解读
方法二:
def Fun1():
x=5
def Fun2():
nonlocal x
x*=x
return x
return Fun2()
print(Fun1())
代码解读
lambda表达式
def add(x,y):
return x+y
print(add(3,4))
# 等价于
g = lambda x,y : x+y
print(g(3,4))
代码解读
lambda表达式的妙用之filter、map函数
filter(function, iterable)
将可迭代序列iterable每个元素依次作为function的参数进行运算,从iterable中筛选出运算结果为TRUE的元素;若function为None,则直接从iterable中筛选出为True的元素。
返回值:指针。
map(function,iterable)
将iterable的每一个元素作为function的参数进行运算加工,直到每个元素都加工完毕,返回所有加工后的元素构成的新序列。
def odd(x):
return x%2
temp = range(10)
show = filter(odd,temp)
print(list(show))
# 等价于
print(list(filter(lambda x: x%2,range(10))))
代码解读
print(list(map(lambda x: x*2,range(10))))
代码解读
递归
递归默认调用层数为100,设置递归调用层数
import sys
sys.setrecursionlimit(10000)
代码解读
阶乘
# 递归求阶乘
def factorial(n):
if n==1:
return 1
else:
return n*factorial(n-1)
number = int(input("请输入一个正整数:"))
result = factorial(number)
print("%d 的阶乘是:%d" % (number,result))
代码解读
斐波那契数列
迭代求解:
# 迭代求斐波那契数列
def fab(n):
n1=1
n2=1
n3=1
if n<1:
print('输入有误!')
return -1
while n-2>0:
n3=n2+n1
n1=n2
n2=n3
n-=1
return n3
print('总共有 %d 对小兔子!' % fab(20))
代码解读
递归求解
def fab(n):
if n<1:
print('输入有误!')
return -1
if n==1 or n==2:
return 1
else:
return fab(n-1)+fab(n-2)
print('总共有 %d 对小兔子!' % fab(20))
代码解读
汉诺塔
# 递归求解汉诺塔
def hanoi(n,x,y,z):
if n==1:
print(x,' --> ',z)
else:
hanoi(n-1,x,z,y) # 将前n-1个盘子从x移动到y上
print(x,' --> ',z) # 将最底下的最后一个盘子从x移动到z上
hanoi(n-1,y,x,z) # 将y上的n-1个盘子移动到z上
n = int(input("请输入汉诺塔的层数:"))
hanoi(n,'X','Y','Z')
代码解读
字典
工厂函数(类型)
dict2 ={1:'one',2:'two',3:'three'}
print(dict2[2])
dict3 = dict((('F',70),('i',105),('s',115)))
print(dict3)
dict4 = dict(小甲鱼='让编程改变',仓='让...征服')
print(dict4)
代码解读
fromkeys
dict1 = {}
print(dict1.fromkeys((1,2,3)))
print(dict1.fromkeys((1,2,3),'Number'))
print(dict1.fromkeys((1,2,3),('one','two','three')))
{1: None, 2: None, 3: None}
{1: 'Number', 2: 'Number', 3: 'Number'}
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
代码解读
dict1 = dict1.fromkeys(range(32),'赞')
# for eachKey in dict1.keys(): # 键,值为values
# print(eachKey)
#
# for eachItem in dict1.items(): # 项
# print(eachItem)
dict1.get(32) # 返回None,根据键查找
dict1.get(32,'木有!') # 默认不存在键32则返回 '木有!'
print(32 in dict1) # 判断键32是否在字典dict1中
dict1.clear() # 清空,推荐做法
dict1 = {} # 不推荐,并不会销毁
代码解读
a={1:'one',2:'two',3:'three'}
b=a.copy() # 浅拷贝与赋值不同,浅拷贝也是拷贝,赋值只是贴了个标签
c=a
print(id(a),id(b),id(c))
print(a.pop(2)) # two
print(a)
print(a.popitem()) # 随机弹出一个项 (3, 'three')
print(a)
print(a.setdefault('小白')) # None
print(a)
print(a.setdefault(5,'five')) # five
print(a)
b={'小白':'狗'}
a.update(b)
print(a)
1991383392152 1991383421000 1991383392152
two
{1: 'one', 3: 'three'}
(3, 'three')
{1: 'one'}
None
{1: 'one', '小白': None}
five
{1: 'one', '小白': None, 5: 'five'}
{1: 'one', '小白': '狗', 5: 'five'}
代码解读
集合
# 集合
num2 ={1,2,3,4,5}
# 唯一性,会自动去掉重复数据,没有顺序,不支持索引
set1 = set([1,2,3,4,5,5])
# in 和 not in判断元素是否在集合中
print(1 in num2)
num2.add(6)
num2.remove(5)
num3 = frozenset([1,2,3,4,5]) # 不可变集合
num3.add(0) # 报错
代码解读
文件
f = open('E:\ Projects\ Python\ record.txt')
print(f.readline())
print(f.read()) # 全部读出来
f.seek(0,0)
lines = list(f)
for each_line in lines: # 先转换为列表,再打印每一行,效率低
print(each_line)
f.seek(0,0)
for each_line in f: # 效率高
print(each_line)
(role,line_spoken)=each_line.split(':',1)
f.close()
代码解读
全部评论 (0)
还没有任何评论哟~
