1. 是不是某类型?isinstance
a = 1
b = 0.5
isinstance(a, int)
True
isinstance(b, int)
False
isinstance(b, (int, float))
True
2. 多行字符串——三引号
- 换行占字符
3. 计算某字符个数 c.count(‘a’)
'12345654321'.count('3')
2
4. 转义符
print('\\\\\\')
\\\
5. 时期和日期
-
%Y 四位年
-
%y 两位年
-
%m 月
-
%d 日
-
%H 24小时制的时
-
%I 12小时制
-
%M 两位分钟
-
%s 秒
-
%w 星期几
-
%U 一年中的第几个星期,星期天是每周第一天
-
%W 一年中的第几个星期,星期一是每周第一天
-
%z UTC时区
-
%F 按照‘200-11-17’格式
-
%d 按照‘04/18/12’(m/d/y)
-
改变日期格式,注意大小些分别代表啥
-
把字符串改变成日期对象
-
计算两个时间相差多久
import datetime
from datetime import datetime, date, time
# Y m d - H M S
dt = datetime(2000, 11, 17, 3, 46, 23)
dt
datetime.datetime(2000, 11, 17, 3, 46, 23)
dt.day
17
dt.minute
46
dt.date()
datetime.date(2000, 11, 17)
dt.time()
datetime.time(3, 46, 23)
# 改变格式,注意大小些分别代表啥
dt.strftime(r'%m/%d/%Y %H:%M:%s')
'11/17/2000 03:46:23'
# 把字符串改变成日期对象
datetime.strptime('2012:03:17', '%Y:%m:%d')
datetime.datetime(2012, 3, 17, 0, 0)
dt = dt.replace(minute = 0, second = 0)
dt
datetime.datetime(2000, 11, 17, 3, 0)
# 计算两个时间相差多久
dt1 = datetime(2000, 11, 17, 3, 46, 23)
dt2 = datetime(2012, 3, 17, 11, 46, 23)
delta = dt2 - dt1
delta
datetime.timedelta(days=4138, seconds=28800)
6. 元组
# 转换其他类型到元组
tuple('abcde')
('a', 'b', 'c', 'd', 'e')
# 元组可加,如果元素只有一个,那么就需要加逗号
test = (4, None, 'mdx') + (5, True) + ('lby',)
test
(4, None, 'mdx', 5, True, 'lby')
# 元组乘法
test * 2
(4, None, 'mdx', 5, True, 'lby', 4, None, 'mdx', 5, True, 'lby')
# 元组查个数
test.count(4)
1
7. 列表
列表一次添加多个元素
x = [5, False, 'hhh']
# 列表一次添加多个元素
x.extend((7, 8, [2, 3]))
x
[5, False, 'hhh', 7, 8, [2, 3]]
列表排序
列表按字符长度排序
sorted可以排序字符串,可以直接变字符串转数组
# 列表按字符长度排序
x = ['a', 'bb', 'yyy', '01']
x.sort(key = len)
print(x)
['a', 'bb', '01', 'yyy']
# sorted可以排序字符串,可以直接变字符串转数组
sorted('I love Hongkong!')
[' ',
' ',
'!',
'H',
'I',
'e',
'g',
'g',
'k',
'l',
'n',
'n',
'o',
'o',
'o',
'v']
# 这个就不行
'I love u'.sort()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/var/folders/9l/fdqtn9lj3mqd1mr7rsbr_qlc0000gn/T/ipykernel_41485/288801555.py in <module>
1 # 这个就不行
----> 2 'I love u'.sort()
AttributeError: 'str' object has no attribute 'sort'
arr = list('Iloveu')
p = arr.sort()
p
列表二分搜索和已排序列表的维护
import bisect
c = [1, 2, 2, 2, 3, 4, 7]
c中,2这个数字应该放到第几位,不会真的插,只返回位置
bisect.bisect(c, 2)
4
把元素插入第几位,真的插进去了
print(bisect.insort(c, 6))
c
None
[1, 2, 2, 2, 3, 4, 6, 6, 7]
切片
利用切片进行数组反转
c[::-1]
[7, 6, 4, 3, 2, 2, 2, 1]
zip函数的特性
自动取短,故不限长度
Z1 = zip([1, 2, 3], ['a', 'b', 'c', 'd'])
print(list(Z1))
[(1, 'a'), (2, 'b'), (3, 'c')]
reversed函数
list(reversed(range(10)))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
8. 字典
字典的删除
a = dict({0:'a', 1:'b', 2:'c'})
del a[1]
print(a)
a.pop(0)
{0: 'a', 2: 'c'}
'a'
keys() and values()迭代器
a = dict({0:'a', 1:'b', 2:'c'})
print(a.keys())
print(a.values())
dict_keys([0, 1, 2])
dict_values(['a', 'b', 'c'])
字典的更新操作,dict.update({})函数
- 相同的被覆盖
a.update({3:'d',4:'e', 0:'f'})
a
{0: 'f', 1: 'b', 2: 'c', 3: 'd', 4: 'e'}
用序列来造/合成一个字典
key_list = [0, 1, 2]
value_list = ['a', 'b', 'c']
mapping = dict()
for k, v in zip(key_list, value_list):
mapping[k] = v
mapping
{0: 'a', 1: 'b', 2: 'c'}
用序列来造/合成一个字典的简单操作
mapping_2 = dict(zip(key_list, value_list))
mapping_2
{0: 'a', 1: 'b', 2: 'c'}
dict.get函数
字典里有没有某个key,如果有就返回对应的value,如果没有,就返回自定义的默认值
mapping_2.get(1, 'No')
'b'
mapping_2.get(4, 'No')
'No'
dict.setdefault函数
dict_1 = {0:'a', 1:'b', 2:'c'}
dict_1.setdefault(3, None)
dict_1
{0: 'a', 1: 'b', 2: 'c', 3: None}
# 如果有,就返回所对应的value
dict_1.setdefault(2, None)
print(dict_1)
# 可以这么用
dict_1.setdefault(5, []).extend([2, 4, 5])
dict_1
{0: 'a', 1: 'b', 2: 'c', 3: None}
{0: 'a', 1: 'b', 2: 'c', 3: None, 5: [2, 4, 5]}
hash函数 hash()
- 只能放不可变的类型,可变会报错,unhashable
hash('string!')
-1949024158785360325
9. set
- 集合合并:用于并查集的union
- 集合相交intersection
- 集合相减difference
- 不同时在两个集合symmetric_difference,符合表示^
- 是否是子集issubset
- 是否是包含issuperset
- 是否有交集isdisjoint
Set = {1, 2, 3}
print(Set.union({0, 4})) # 本身没有变
Set | {2, 3, 5}
{0, 1, 2, 3, 4}
{1, 2, 3, 5}
- 交集:intersection()
print(Set.intersection({3, 4, 5}))
Set & {3, 4, 5}
{3}
{3}
- 减法:difference()
- 在a不在b的操作
- 其实就是a-b
Set.difference({2, 3, 5})
{1}
- 不同时在a和b:symmetric_difference()
- ^
Set.symmetric_difference({3, 4, 5})
{1, 2, 4, 5}
Set ^ {3, 4, 5}
{1, 2, 4, 5}
- 判断是否是子集
{2, 3}.issubset(Set)
True
- 判断集合包含关系
Set.issuperset({2, 3})
True
- 判断是否没有交集
Set.isdisjoint({2, 3})
False
map()函数
list(map(len, ['abc', 'a', '22', '1']))
[3, 1, 2, 1]
def cal_square(n):
return n**2
list(map(cal_square, [1, 2, 3, 4]))
[1, 4, 9, 16]
10. 字符串
- 变大写
- 字符串每个单词首字母大写
- 一句话的首字母大写
- 大小写转换
- 去掉前后空格
'a'.upper()
'A'
字符串每个单词首字母大写
'this is a title'.title()
'This Is A Title'
一句话的首字母大写
'a string only change its head'.capitalize()
'A string only change its head'
大小写转换
'What A Shit!'.swapcase()
'wHAT a sHIT!'
去掉前后空格
' abs'.strip()
'abs'
11. 推导式
Strings = ['a', 'bb', 'lby', 'what??']
[{x:x.upper()} for x in Strings if len(x) >= 3]
[{'lby': 'LBY'}, {'what??': 'WHAT??'}]
12. 函数
- global全局变量
- 外面没有某变量,但在函数内部声明global,那么在外部再次使用的时候,就还是那个
- 外面有某个变量,但没说是global的。内部想使用这个变量,那就需要在函数内部声明一下【global 某变量】
13. 正则表达式RE
函数
- \d
- a*
- [0-9] 或的关系
- re.match(pattern, string, flags = 0)
- 匹配开头,能匹配上就返回值,不能就返回None
- pattern: 模式,比如a* \d str = ‘([0-9])([a-z])([0-9]*)’
- string: 待匹配的字符
- flag标志位,比如是否区分大小写
- re.search(pattern, string, flags = 0)
- 扫描整个字符串,匹配成功,就返回第一个匹配成功的对象,否则返回None
- re.sub(parttern, repl, string, count = 0, flags = 0)
- re.compile
- 编辑pattern
- findall
- 返回所有,是一个列表
- finditer
- 返回一个迭代器的形式
-
re.split
- (?P)
通配符Pattern
- ‘^abc’ 以abc为开头
- ‘abc+dollar符’ 以abc结尾
- ‘.’ 任意字符串,除了\n
- ‘a|1’ 只要有a或有1,就匹配了
- ‘ab{2}c’ 只有2个b的可以
- ‘[Aa][Ll]ien’ A也行a也行
- ‘()’ 一个逻辑分组,就是搞优先级
- ‘A*’ 0个或多个A
- ‘A+’ 1个或多个A
- ‘ab*?’ 有多个可以匹配的时候,选择最少最少的那个。在这里,能没有b就没有b
- [^aeIoU] 不匹配aeIoU
- \D 非数字
- \w 匹配所有字母和0-9
- \W 非字符,跟\w反着
- \s 匹配所有空白字符,换页换行回撤Tab等
- \S 上面的反
修饰符,就是那个flags
- re.I 不考虑大小写
- re.L 本地化识别匹配
- re.M 多行匹配
re.match( )
import re
index = re.match('aac', 'aac123')
print(index)
print(re.match('a', 'bac123'))
<re.Match object; span=(0, 3), match='aac'>
None
if index:
print(index.start())
print(index.end())
print(index.span())
print(index.group())
0
3
(0, 3)
aac
ans = re.match('([0-9])([a-z]*)([0-9]*)', '123abc456')
# 打印匹配结果
print(ans.group(0))
# 和第一个括号匹配的结果
print(ans.group(1))
# 和第二个括号匹配的结果
print(ans.group(2))
# 和第三个括号匹配的结果
print(ans.group(3))
# 没有第四个,所以使用的话会报错
123
1
23
re.search( )
Search = re.search('com', 'www.comdu.com')
print(Search.start())
print(Search.end())
print(Search.span())
4
7
(4, 7)
re.sub( )
s_sub = "123 abc 456 456 456"
p_sub = '456'
r_sub = '789'
re.sub(p_sub, r_sub, s_sub)
'123 abc 789 789 789'
# 替换一次
re.sub(p_sub, r_sub, s_sub, count = 1)
'123 abc 789 456 456'
# r_sub是函数
def double(x):
print(type(x))
value = int(x.group())
return str(value * 2)
s = '12'
print(re.sub('\d', double, s))
<class 're.Match'>
<class 're.Match'>
24
re.compile(’ ')
# 制作pattern的
p_findall = re.compile(r'\d+')
re.findall( )
result1 = p_findall.findall('123abc456', 3, 8)
result1
['45']
result2 = p_findall.findall('123abc456')
result2
['123', '456']
re.findall(p_findall, '123abc456')
['123', '456']
re.split( )
- 按照能够匹配的子串将字符串分割后返回列表
- '\w’非字母数字下划线
re.split('\W+',', runoob, runoob, runoob. ')
['', 'runoob', 'runoob', 'runoob', '']
‘(?P)’
- 分组匹配
- res.groupdict() 将其转化成字典打印
s = '21011120001117252X'
res = re.search('(?P<province>\d{3})(?P<city>\d{3})(?P<birth_year>\d{4})', s)
print(res)
res = res.groupdict()
res
<re.Match object; span=(0, 10), match='2101112000'>
{'province': '210', 'city': '111', 'birth_year': '2000'}
14. 匿名函数
y = lambda x:x**2
y(2)
4
def apply_to_list(some_list, f):
return [f(x) for x in some_list]
f = y
apply_to_list([4, 0, 1, 5, 6], f)
[16, 0, 1, 25, 36]
strings = ['foo', 'card', 'bar', 'aaaa', 'abab']
# 按照每个字符串中不重复的字符个数来排序
strings.sort(key = lambda x:len(set(x)))
strings
['aaaa', 'foo', 'abab', 'bar', 'card']
sorted( )和list.sort( )
Currying:Partial Argument Application
柯里化:部分参数的应用
def add_numbers(x, y):
return x+y
add_five = lambda y:add_numbers(5, y)
add_five(3)
8
15. 迭代器iterator
some_dict = {1:'a', 2:'b', 3:'c'}
dict_iter = iter(some_dict.values())
print(list(dict_iter))
# 但凡输出,就自动释放,得需要用个别的来存储
for i in dict_iter:
print(i)
['a', 'b', 'c']
16. 生成器yiel
def squares(n = 10):
for i in range(n+1):
yield i**2
gen = squares()
gen # 这是一个可迭代的东西
<generator object squares at 0x106ca32e0>
# 也是一次性的
for i in gen:
print(i, end = ' ')
0 1 4 9 16 25 36 49 64 81 100
list.index( )
# 返回第一个的下标
[1, 2, 2, 2, 2, 3, 4, 5, 5].index(2)
1
17. itertools工具箱
groupby功能
from itertools import groupby
test = [(1, 5), (1, 4), (1, 3), (1, 2), (2, 4), (2, 3), (3, 5)]
temp = groupby(test, key = lambda x:x[0])
for i, v in temp:
print(i, list(v))
1 [(1, 5), (1, 4), (1, 3), (1, 2)]
2 [(2, 4), (2, 3)]
3 [(3, 5)]
实现排列组合
实现排列组合中的组合功能,不考虑顺序
from itertools import combinations
test = combinations([1, 2, 3, 4], 3)
print(list(test))
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
实现排列组合中的排列功能,不考虑顺序
from itertools import permutations
test = permutations([1, 2, 3, 4], 3)
print(list(test))
[(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)]
两个对象的组合功能,有点像product功能(其实就是product功能)
from itertools import product
test = product('abc', 'xy')
print(list(test))
[('a', 'x'), ('a', 'y'), ('b', 'x'), ('b', 'y'), ('c', 'x'), ('c', 'y')]
18. 错误和异常处理
try except
def transfer_float(x):
try:
return float(x)
except:
return x, 'cannot transfer'
transfer_float('what?!')
('what?!', 'cannot transfer')
except AError:回避指定错误
def transfer_float(x):
try:
return float(x)
except TypeError:
return x, 'cannot transfer this type'
transfer_float((1, 2))
((1, 2), 'cannot transfer this type')
# 这种value error就还是会报错
transfer_float('What?!')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/var/folders/9l/fdqtn9lj3mqd1mr7rsbr_qlc0000gn/T/ipykernel_41485/2101229274.py in <module>
1 # 这种value error就还是会报错
----> 2 transfer_float('What?!')
/var/folders/9l/fdqtn9lj3mqd1mr7rsbr_qlc0000gn/T/ipykernel_41485/1586127845.py in transfer_float(x)
1 def transfer_float(x):
2 try:
----> 3 return float(x)
4 except TypeError:
5 return x, 'cannot transfer this type'
ValueError: Could not convert string to float: 'What?!'
def transfer_float(x):
try:
return float(x)
except (TypeError, ValueError):
return x, 'cannot transfer this type'
transfer_float('What?!')
('What?!', 'cannot transfer this type')
一个比较全的例子
def example():
try:
1/0
except ZeroDivisionError:
print("GetAError") # 指定一种报错
except:
print("Exception") # 全部报错
else:
print('try is success!') # try成功执行 else语句必须至少有一个except为前提
finally:
print('no matter try is success or not, process the finally') # try成不成功都需要用这个
example()
GetAError
no matter try is success or not, process the finally
19. 文件操作
检验文件的默认编码
import sys
sys.getdefaultencoding()
'utf-8'
文件打开模式