1 基础语法
Python的八种数据类型
八种数据类型分别是:
number(数字)、string(字符串)、Boolean(布尔值)、None(空值)
list(列表)、tuple(元组)、dict(字典)、set(集合)。
## 可变类型:列表,字典,集合————》 在内存中是以链表的形式存储,每个元素都有独立的地址和地址指向,可以直接修改 ## 不可变类型:数字,字符串,元祖
## 有序:字符串,列表,元组
## 无序:集合
字典dict在python3.6之前是无序的,到了python3.7变成了有序,可变
集合set无序可变(还有个不可变集合frozenset)
1.1列表
1.1.0 列表的存储方式(数据结构)
即整个列表包括三部分:列表类型、列表长度、列表元素的地址
地址指向每个元素对象,每个元素对象包括:类型、元素
列表中的元素在内存中是不连续的。
1.1.1 列表常用函数及方法
1.1.1.1常用函数
cmp(list1, list2)
# 比较两个列表的元素
len(list)
# 列表元素个数
max(list)
# 返回列表元素最大值
min(list)
# 返回列表元素最小值
list(seq)
# 将元组转换为列表
1.1.1.2常用方法
list.append(obj)
# 在列表末尾添加新的对象
list.count(obj)
# 统计某个元素在列表中出现的次数
list.extend(seq)
# 在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
list.index(obj)
# 从列表中找出某个值第一个匹配项的索引位置
list.insert(index, obj)
# 将对象插入列表
list.pop([index=-1])
# 移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
list.remove(obj)
# 移除列表中某个值的第一个匹配项
list.reverse()
# 反向列表中元素
list.sort(cmp=None, key=None, reverse=False)
# 对原列表进行排序
示例:列表筛选小于指定值
def filter_element(items, bound):
res = [item for item in items if item < bound]
return res
示例:列表推导式
if __name__ == '__main__':
obj_list = [
{"key": "day1", "value": "大雨哗啦啦啦啦下", 'tags': ["不热"]},
{"key": "day2", "value": "很热很热,一个人旅行", 'tags': ["热"]},
{"key": "day3", "value": "阴天有时下雨", 'tags': ["不热"]}
]
print('')
print("# 过滤出不热的日子")
#!!!!!!!!!!!
non_hot_days = [d for d in obj_list if '不热' in d['tags']]
for day in non_hot_days:
print("* [{}]: {}".format(day['key'], day['value']))
print('')
1.1.1.3切片
str = 'abcdefg'
# 索引为 0表示第一个,1表示第二个,-1表示最后一个,-2表示倒数第二个
print(str[1:]) # 从下标1到最后,包括最后一个元素
print(str[:-1]) # 从头到-1左闭右开区间,即不包括-1最后一个元素
print(str[-1:]) # 从-1到最后,即获取最后一个元素
print(str[::-1]) # 从头到尾,但是逆序,步长为1
print(str[::-2]) # 从头到尾,但是逆序,步长为2
print(str[:]) # 从头到尾
print(str[:1:-1]) # 从头到下标1结束,但是逆序,即头是最后一个,从最后到下标1左闭右开
print(str[1::-1]) # 从下标1到尾,但是逆序,即尾是第一个,从下标1到最左左闭右闭
print(str[2:0:-1]) # 逆序,尾下标一定要小于头下标,即下标仍按照从左往右,但是输出是右往左
print(str[:-2:-1]) # 逆序,-1到-2左闭右开
print(str[-1:]) # 从下标-1到最后
-------------------------------------------------------------------------------------------
结果
bcdefg
abcdef
g
gfedcba
geca
abcdefg
gfedc
ba
cb
g
g
1.1.2 list实现栈——先进后出
“后进先出”(Last-In-First-Out,LIFO)
【Python】一文带你学会数据结构中的堆、栈_python stack-CSDN博客
# 初始化栈
stack = []
# 压栈(入栈)
stack.append(1)
stack.append(2)
stack.append(3)
# 弹栈(出栈)
element = stack.pop()
print(element) # 输出:3
# 获取栈顶元素
top_element = stack[-1]
print(top_element) # 输出:2
# 判断栈是否为空
is_empty = len(stack) == 0
print(is_empty) # 输出:False
# 显示栈的元素
for element in stack:
print(element)
# 输出:
# 1
# 2
1.1.3字符串
# python 三引号允许一个字符串跨多行,字符串中可以包含换行符、制表符以及其他特殊字符。
string = '''abd
efgd'''
print("双引号字符串里的单引号: 'hello world!'")
print('单引号字符串里的双引号: "hello world!"')
1.1.3.1字符串拼接
#用list索引拼接
str_list = ["Hello,","World!"]
print(str_list[0]+str_list[1])
#字符串拼接
str1 = "Hello,"
str2 = "World!"
print(str1+str2)
1.1.3.2字符串拆分
split()方法返回一个列表,其中包含分割后的子字符串。
string.split(separator, maxsplit)
separator:可选参数,指定分隔符,默认为空格。可以是一个字符或字符串,用于指定在哪里分割字符串。
maxsplit:可选参数,指定最大分割次数。如果提供了该参数,则最多分割成maxsplit+1个子字符串。如果没有提供该参数或者为-1,则分割次数没有限制
字符串拆分示例示例:
creators = ["约翰·巴科斯(JohnWarnerBackus), 创建了Fortran语言", "阿兰·库珀(Alan Cooper), 开发了Visual Basic语言"]
def parse_parts(creator):
# .find() 找字符串中的指定元素,返回该元素的下标
index = str_list.find(',')
# list列表切片左开右闭,第一个元素下标0,最后一个元素是-1
name, achievement = creator[0:index], creator[index+1:]
name_cn, name_en = name[0:index], name[index:]
name_en = name_en[1:len(name_en)-1]
# .strip() 移除字符串首尾的指定字符,默认空格和换行符
return name_cn, name_en, achievement.strip()
#parse_creators的第一种写法
def parse_profile(creator):
name, achievement = parse_parts(creator)
name_cn, name_en = parse_name(name)
#组成字典{}
return { 'name_cn': name_cn, 'name_en': name_en, 'achievement': achievement }
def parse_creators(creators):
#每次函数回调都会给[]列表添加一个元素,元素是字典类型
return [ parse_profile(creator) for creator in creators]
#parse_creators的第二种写法
def parse_creators(creators):
profiles = []
for creator in creators:
name_cn, name_en, achievement = parse_parts(creator)
# .append() 给list末尾添加元素
profiles.append({ 'name_cn': name_cn, 'name_en': name_en, 'achievement': achievement })
return profiles
if __name__ == '__main__':
creators = ["约翰·巴科斯(JohnWarnerBackus), 创建了Fortran语言", "阿兰·库珀(Alan Cooper), 开发了Visual Basic语言"]
profiles = parse_creators(creators)
print(profiles)
示例:字符串转列表
str="1,2,34"
print(list(str.split(',')))
# ['1', '2', '34']
string = "Hello, world! How are you?"
result = string.split()
print(result)
# ['Hello,', 'world!', 'How', 'are', 'you?']
示例:字符串列表转数值列表
Python列表字符转为数值_如何将列表中的字符串转化为数字的形式输出-CSDN博客
num_list_new = list(map(lambda x : int(x),new_list))
1.1.3.3拼接字符串
8种Python字符串拼接的方法,你知道几种?-CSDN博客
.format拼接字符串:
text1 = 'hello'
text2 = 'world'
print("{}{}".format(text1, text2))
1.1.3.4f字符串
在字符串中嵌入变量,基本用法:
name = "Tom"
age = 20
print(f"My name is {name}, and I am {age} years old.")
详解如何理解并正确使用Python中的f字符串_python_脚本之家
1.1.3.5字符串排序
字符串排序(Python)_python 字符串排序-CSDN博客
str1 = "shd"
print(sorted(str1))# 升序
print(str1)
print("".join(sorted(str1)))# 串成字符串
print(sorted(str1,reverse=True))# 降序
___________________________________________
['d', 'h', 's']
shd
dhs
['s', 'h', 'd']
1.1.4一些函数
1.1.4.1replace
Python replace()函数使用详解,Python替换字符串_python string replace-CSDN博客
#将string字符串中的‘old’替换为‘new’,count指定替换次数
string.replace( old, new, count )
# 去除空格
string.replace(" ","")
1.1.4.2lower
# 将字符串中的所有大写字母转换为小写字母
# lower()方法不会改变原始字符串,而是返回一个新的字符串。
a = "HELLO"
print(a.lower())
1.1.4.3split
Python 中的字符串分割函数 split() 详解_python split-CSDN博客
# split() 通过指定分隔符对字符串进行切片,并以列表的形式返回这些子字符串。
text = "Hello,World,Python"
words = text.split(",") # 使用逗号作为分隔符
print(words) # 输出:['Hello', 'World', 'Python']
1.1.4.4list
Python:list()函数(列表[ ])、dict()函数(字典{ })_list()返回什么-CSDN博客
# 将字典的所有value转换成列表
word_list = list(word_dict.values())
1.1.4.5join
详解Python中的join()函数的用法_python_脚本之家
# 将char列表内的字母拼成小写字符串
word = ''.join(chars).lower()
#对序列进行操作(分别使用' '与':'作为分隔符)
seq1 = ['hello','good','boy','doiido']
print(' '.join(seq1))
#hello good boy doiido
print(':'.join(seq1)
#hello:good:boy:doiido
#合并目录
import os
os.path.join('/hello/','good/boy/','doiido')
#'/hello/good/boy/doiido'
1.1.4.6sort
lambda的一些说明:
vowels = ['e', 'a', 'u', 'o', 'i']
# 降序
vowels.sort(reverse=True)
#['u', 'o', 'i', 'e', 'a']
#升序
vowels.sort()
# lambda指定按照x的哪个元素进行比较排序
x = [
('john', 'A', 15),
('jane', 'B', 12),
('dave', 'B', 10),
('dave', 'B', 8),
]
x.sort(key=lambda i:i[2])
print(x)
# [('dave', 'B', 8), ('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
1.1.4.7 find
Python中find函数的详细使用方法_python_脚本之家
s = "abcdefg"
print(s.find("def"))# 3
1.1.4.8 index查下标
python的index是什么意思-Python教程-PHP中文网
找到返回索引,找不到引发异常
def find_index():
s = "abcdefg"
try:
return print(s.index("def"))
except ValueError:
return print(-1)
find_index()
# 3
——————————————————————————————————
def find_index():
s = "abcdefg"
try:
return print(s.index("fc"))
except ValueError:
return print(-1)
find_index()
# -1
1.1.5 lambda匿名函数
Python中lambda 函数详解与运用_key=lamdba x: x[n]-CSDN博客
lambda x, y: x+y # 函数输入是x和y,输出是它们的和x+y
lambda:None # 函数没有输入参数,输出是None
lambda *args: sum(args) # 输入是任意个数参数,输出是它们的和(隐性要求输入参数必须能进行算术运算)
lambda **kwargs: 6 # 输入是任意键值对参数,输出是6
lambda item: item['age'] # 输入字典item,输出字典中kay=age对应的value
1.2字典
1.2.1字典的key
python中的类型分可以分为两类,可变类型和不可变类型
可变类型:dict、list、set
不可变类型:int、long、float、complex、string、bool、tuple
其中可变类型不能作为字典的key,因为它们没有__hash__()方法。
1.2.2遍历字典
Python 遍历字典的8种方法总结_python_脚本之家
# 创建一个示例字典
student_grades = {"Alice": 95, "Bob": 88, "Charlie": 92, "David": 78}
# 创建空字典
dic = dict()
# 遍历字典的键
for name in student_grades:
print(name)
# 遍历字典的值
for grade in student_grades.values():
print(grade)
# 遍历字典的键值对
for name, grade in student_grades.items():
print(f"{name}: {grade}")
字典的使用示例:
if __name__ == '__main__':
install = {
"Windows": "请下载 Windows 安装包安装:https://www.python.org/downloads/windows/",
"CentOS": "使用yum包管理器,执行命令:yum install -y python3",
"Ubuntu": "使用apt-get包管理器,执行命令:apt-get install -y python3",
"MacOS": "安装brew包管理器,然后执行命令: brew install python3",
}
shortcut_keys = {}
# 遍历install字典的key
for platform in install:
# 将install的每一个key的第一个字母变成小写,作为shortcut_keys字典的key
key = platform[0].lower()
# shortcut_keys字典的value是install字典的key
shortcut_keys[key] = platform
while True:
ret = input("请选择安装平台[w/c/u/m, 按q退出]:")
if ret == 'q':
break
platform = shortcut_keys.get(ret)
if platform is None:
print("不支持的平台")
else:
doc = install.get(platform)
print(f"{platform}: {doc}")
字典的嵌套示例:
if __name__ == '__main__':
run = {
"repl": {
"title": "交互式编程( Interactive )",
"desc": [
"打开终端,输入 python 回车",
"进入 Python 交互式命令行",
"输入 print('monkey king is coding!')"
]
},
"source": {
"title": "Python 源代源文件( File )",
"desc": [
"使用你喜欢的编辑器拷贝本练习的代码, 保存为run.py",
"打开终端,cd 到 run.py 保存的目录",
"输入 python run.py"
]
}
}
print("有两种基本的方式运行 Python")
for s in run: # 遍历run字典的key:i f
# item是i和f对应的value,本身是一个字典
item = run.get(s)
# item['title'],获取i和f的value字典中,key为title的value
print("* {}: {}".format(s, item['title']))
shortcut_keys = {}
# 遍历install字典的key
for r in run:
# 将install的每一个key的第一个字母变成小写,作为shortcut_keys字典的key
key = r[0].lower()
# shortcut_keys字典的value是install字典的key
shortcut_keys[key] = r
has_learn_repl = False
has_learn_source = False
while True:
ret = input("请选择你想了解的 Python 运行方式(输入:r/s选择,输入 q 退出):")
if ret == 'q':
break
elif ret == 'r':
has_learn_repl = True
elif ret == 's':
has_learn_source = True
name = shortcut_keys.get(ret)
if name is None:
print("[错误] 不支持的运行方式")
else:
item = run.get(name)
desc = item['desc']
for i in range(0, len(desc)):
print("{}. {}".format(i, desc[i]))
if has_learn_repl and has_learn_source:
print("[2/2]您已完成两种 Python 运行方式的学习")
elif has_learn_source:
print("[1/2]您已完成 Python 源代码方式运行学习")
elif has_learn_repl:
print("[1/2]您已完成 Python 交互式命令行运行学习")
else:
print("[0/2]您似乎跳过了运行方式的学习?期待下次光临!")
字典和列表的查询示例:
def parse_ide_trend(top_ide_trend):
top_ides = []
top_ide_lines = top_ide_trend.split('\n')[1:]#第一行是表头,从表头开始分行存到列表里
head = top_ide_lines[0]
columns = head.split('\t') # 将表头分成几个元素存到列表columns
for row in top_ide_lines[1:]:
row = row.strip()
if row:
cells = row.split('\t') #将每一行分割存到cells列表中
top_ide = {}
for i in range(0, len(columns)):
column = columns[i]
cell = cells[i]
top_ide[column] = cell # rank:1,change:visual studio...
top_ides.append(top_ide) # 列表,存的是排名的每一行,但用字典作了对应
return top_ides # 返回一个rank列表,按照rank顺序排列,列表的每一个元素是一个字典,包含了这个元素的排名、名字、百分数等等
def parse_py_ide(py_ide_infos):
py_ides = []
for row in py_ide_infos:# 遍历列表每个元素
name_end = row.find('(')
name = row[0:name_end]
link_end = row.find(')')
link = row[name_end+1:link_end]
desc = row[link_end+2:]
py_ides.append({'name': name, 'link': link, 'desc': desc}) # 字典存储
return py_ides
def dump_join_result(py_ide, top_ide):
py_ide_name = py_ide['name']
print(f'Python IDE:{py_ide_name}')
print('----------')
print('* 基本信息')
for key in py_ide:
value = py_ide[key]
print(f' * {key}: {value}')
print('* 排行信息')
if top_ide: # top_ide是一个字典,是true!!! 此时已经在rank里定位到了指定的ide
for key in top_ide:# 遍历该ide的每个key
if key != 'IDE' and key != 'Change':
value = top_ide[key]
print(f' * {key}: {value}')
else:
print(' * 无')
print('')
if __name__ == '__main__':
top_ide_trend = '''
Rank Change IDE Share Trend
1 Visual Studio 29.24 % +3.5 %
2 Eclipse 13.91 % -2.9 %
3 Visual Studio Code 12.07 % +3.3 %
4 Android Studio 9.13 % -2.5 %
5 pyCharm 8.43 % +0.7 %
6 IntelliJ 6.7 % +0.8 %
7 NetBeans 4.82 % -0.3 %
8 Sublime Text 3.49 % -0.2 %
9 Xcode 3.37 % -1.2 %
10 Atom 3.25 % -0.5 %
11 Code::Blocks 2.16 % +0.2 %
12 Vim 0.79 % -0.1 %
13 Xamarin 0.48 % -0.1 %
14 PhpStorm 0.46 % -0.2 %
15 geany 0.39 % +0.2 %
16 Komodo 0.31 % -0.2 %
17 Qt Creator 0.26 % -0.0 %
18 Emacs 0.18 % -0.1 %
19 JDeveloper 0.13 % -0.0 %
20 RAD Studio 0.08 % -0.0 %
21 JCreator 0.07 % -0.0 %
22 Light Table 0.07 % -0.0 %
23 MonoDevelop 0.06 % -0.0 %
24 SharpDevelop 0.03 % -0.0 %
25 Eric Python 0.03 % -0.0 %
26 Aptana 0.02 % -0.0 %
27 RubyMine 0.02 % -0.0 %
28 Coda 2 0.02 % +0.0 %
29 Monkey Studio 0.01 % -0.0 %
30 DrJava 0.01 % -0.0 %
'''
py_ide_infos = [
"IDEL(https://docs.python.org/3/library/idle.html), Python 内置的IDE,功能比较一般。",
"VIM(http://www.vim.org/),如果你是个VIM爱好者,可以用VIM编写Python,但是Python的缩进处理会比较麻烦。当然,你在 Linux 服务器上的时候有时候就只能用VI/VIM了。",
"Visual Studio Code(https://code.visualstudio.com/),VSCode 对Python的支持非常友好,配合几个插件后几乎是对 Python 开发最友好的IDE了。",
"PyCharm(https://www.jetbrains.com/pycharm/),jetbrains 出品的 PyCharm 也是 Python 开发者常用的IDE。"
]
top_ides = parse_ide_trend(top_ide_trend)
py_ides = parse_py_ide(py_ide_infos)
for py_ide in py_ides:
find_top_ide = None
for top_ide in top_ides:
if py_ide['name'].lower() == top_ide['IDE'].lower():
find_top_ide = top_ide
dump_join_result(py_ide, find_top_ide)
字典统计词频示例:
def top_words(splits, text, top_n=5):
i = 0
word_dict = {}
chars = []
while i < len(text):
c = text[i] # 逐字符读入
if c in splits: # 说明当前单词结束了,应该更新单词出现次数(首次出现保存)
# 过滤掉分隔字符串
# 还没到文件末尾且当前和下一个都是分隔符,则跳到下一个分隔符
while i+1 < len(text) and text[i+1] in splits:
i += 1
# 将分隔符之前出现的字母拼成小写字符串
word = ''.join(chars).lower()
# 统计词频
# 更新单词出现次数(首次出现保存),得到word_dict字典,其value是字典,单词和词频的两个键值对
# 查询字典中是否已经有这个单词了,返回它对应的值value,
# 如果字典中不存在此单词,则返回{'word': word, 'count': 0}这个字典,赋值给word_info
word_info = word_dict.get(word, {'word': word, 'count': 0})
word_info['count'] += 1
word_dict[word] = word_info
# 以下代码是错误的!!!
# 假设文本是a b a,运行到第二个a时,a已经存在,跳过if,但word_info仍然指向上一次if的单词(即上一次新出现的b)
# 因此会给b的词频+1。
# 其他的第一句是:**word_info = word_dict.get(word)**。
# 作用是将word_info指向word_dict中对应的值上。
# 缺少这一句的话,word_info会指向上一次if的位置。
# if not word in word_dict:
# word_info = {'word': word, 'count': 0}
# word_dict[word] = word_info
# word_info['count'] += 1
chars = []
else:
# 是单词的一部分,加入
chars.append(c)
i += 1
# 将word_dict字典的值转换成列表,这里需要word_dict的value也是字典,即value字典的key有word和count
word_list = list(word_dict.values())
# word_list是一个列表,每个元素是一个字典,每个字典有两个key:word和count
top_n = min(top_n, len(word_list))
# 按count排序,降序,lambda按照列表中的每个元素的count进行比较排序
word_list.sort(key=lambda word_info: word_info['count'], reverse=True)
return word_list[0:top_n] #返回的是列表,列表的元素是字典单词:词频,按rank排序
if __name__ == '__main__':
google_style_guide = '''
Every major open-source project has its own style guide: a set of conventions (sometimes arbitrary) about how to write code for that project. It is much easier to understand a large codebase when all the code in it is in a consistent style.
“Style” covers a lot of ground, from “use camelCase for variable names” to “never use global variables” to “never use exceptions.” This project (google/styleguide) links to the style guidelines we use for Google code. If you are modifying a project that originated at Google, you may be pointed to this page to see the style guides that apply to that project.
This project holds the C++ Style Guide, C# Style Guide, Swift Style Guide, Objective-C Style Guide, Java Style Guide, Python Style Guide, R Style Guide, Shell Style Guide, HTML/CSS Style Guide, JavaScript Style Guide, TypeScript Style Guide, AngularJS Style Guide, Common Lisp Style Guide, and Vimscript Style Guide. This project also contains cpplint, a tool to assist with style guide compliance, and google-c-style.el, an Emacs settings file for Google style.
'''
python_style_guides = '''
* Python 代码风格指南',
* [google-python-styleguide_zh_cn](https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_style_rules /)
* [PEP8](https://legacy.python.org/dev/peps/pep-0008/)
* 代码风格和自动完成工具链
* 基本工具
* [pylint](https://pylint.org/)
* [autopep8](https://pypi.org/project/autopep8/)
* Visual Studio Code Python 开发基本插件
* Pylance
* Python Path
* Python-autopep8
'''
splits = [' ', '-', ':', '/', '*', '_', '(', ')', '"', '”', '“']
# 字符串用+拼接起来了
tops = top_words(splits, google_style_guide+python_style_guides)
print('单词排行榜')
print('--------')
i = 0
while i < len(tops):
top = tops[i] # 排行榜列表嵌套字典,单词是key,词频是value
word = top['word']
count = top['count']
print(f'{i+1}. 单词:{word}, 词频:{count}')
i += 1
1.2.3字典的常用函数
Python中dict字典使用方法_python dict values-CSDN博客
dict.clear() 删除字典中所有元素
dict.copy() 返回字典(浅复制)的一个副本
dict.get(key,default=None) 对字典dict中的键key,返回它对应的值value,如果字典中不存在此键,则返回default 的值(注意,参数default 的默认值为None)
dict.has_key(key) 如果键(key)在字典中存在,返回True,否则返回False
dict.items() 返回一个包含字典中(键, 值)对元组的列表
dict.keys() 返回一个包含字典中键的列表
dict.values() 返回一个包含字典中所有值的列表
dict.pop(key[, default]) 和方法get()相似,如果字典中key 键存在,删除并返回dict[key],如果key 键不存在,且没有给出default 的值,引发KeyError 异常。
1.2.4字典推导式
# condition成立,就将字典dict的键和值赋值给key_new和value_new,并生成新的字典。
{key_new:value_new for key,value in dict.items() if condition}
# condition成立,就将字典dict的键和值赋值给key_new和value_new,并生成新的字典。
# 若不成立,则将value_new2赋给新生成字典的值。
{key_new:value_new if condition else value_new2 for key,value in dict.items()}
1.2.5 defaultdict
Python中的defaultdict方法_python defaultdict-CSDN博客
详解Python中defaultdict的具体使用_python_脚本之家
defaultdict其他功能与dict相同,但会为一个不存在的键提供默认值,如int,set,str,list函数等,从而避免KeyError异常。
from collections import defaultdict
s_dict = defaultdict(int)
s_dict['x'] += 1
print(s_dict)
# defaultdict(<class 'int'>, {'x': 1})
1.2.6 counter计数器
是dict的子类
详解Python中非常好用的计数器Counter_python全局计数器-CSDN博客
from collections import Counter
nums = [1, 2, 3, 4, 5, 1, 2, 3]
print(Counter(nums))
# Counter({1: 2, 2: 2, 3: 2, 4: 1, 5: 1})
# 可以用dict的遍历方法
# eg:查询键是否存在
if 5 in Counter(nums):
print(Counter(nums)[5])
# 1
1.3元组
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5 )
tup3 = "a", "b", "c", "d"
# 元组中只包含一个元素时,需要在元素后面添加逗号
tup1 = (50,)
1.4集合
集合是无序且无重复的
Python数据结构——集合_python 集合-CSDN博客
1.4.1 集合常用函数
示例:去除列表重复
def remove_duplicates(items):
res = list(set(items))
return res
1.4.2 交集并集差集
Python集合&运算符_python集合运算符-CSDN博客
# 交并差
a={1,2,3,11,22}
b={11,22,33,44,55,1}
# 交集 &
s=a&b
print(s)
输出:{1, 11, 22}
# 并集 |
s=a|b
print(s)
输出:{1, 2, 3, 33, 11, 44, 22, 55}
# 差集 -
s=a-b
print(s)
输出:{2, 3}
1.4.3 TypeError: unhashable type: 'list'
Python: TypeError: unhashable type: ‘list‘_typeerror: unhashable type: 'list-CSDN博客
这个错误通常意味着你试图将一个list对象用作哈希参数(hash argument),有以下两种典型(常见)的情况:
(1) 用作字典的键值
(2) 用作集合set的元素
myDict = {[1,2,3]:'123', 'name':'chenxy'}# 错误 列表当key
mySet = set()
mySet.add([1,2,3])# 错误 列表当做set元素
1.5 输入输出
1.5.1print的用法
#print的,起到cpp中<<的作用
#range左开右闭
list1 = ["Go", "AB"]
list2 = [1951, 1980]
[print(list1[i], ':', list2[i]) for i in range(0, len(list1))]
1.5.2input
python的input函数用法_python input-CSDN博客
a = input('请输入字符串:')
a = int(input('请输入数值:')) #输入数值,并用int转为整数,为变量a赋值
1.6 常用库
python_modules = [
"os --- 多种操作系统接口",
"os.path --- 常用路径操作",
"re --- 正则表达式操作",
"datetime --- 基本日期和时间类型",
"heapq --- 堆队列算法",
"enum --- 对枚举的支持",
"math --- 数学函数",
"random --- 生成伪随机数",
"itertools --- 为高效循环而创建迭代器的函数",
"functools --- 高阶函数和可调用对象上的操作",
"shutil --- 高阶文件操作",
"sqlite3 --- SQLite 数据库 DB-API 2.0 接口模块",
"csv --- CSV 文件读写",
"hashlib --- 安全哈希与消息摘要",
"hmac --- 基于密钥的消息验证",
"time --- 时间的访问和转换",
"argparse --- 命令行选项、参数和子命令解析器",
"logging --- Python 的日志记录工具",
"threading --- 基于线程的并行",
"multiprocessing --- 基于进程的并行",
"socket --- 底层网络接口",
"email --- 电子邮件与 MIME 处理包",
"json --- JSON 编码和解码器",
"urllib --- URL 处理模块",
"http --- HTTP 模块"
]
1.6.1time、datetime
time库:
Python中time()模块详解_python time-CSDN博客
获取系统时间的库
#time.time():返回当前时间的时间戳。
import time
if __name__ == '__main__':
print(time.time())# 1717722648.1859927
print(type(time.time()))# <class 'float'>
# import time.time as time # 错误
# time.time 点后面的这个time不是包(只有包才能被点出来,否则需要from package import moudle),是一个方法/函数
# 具体可以使用 import time, dir(time),即可查看(交互式python免去print)
from time import time # 正确
if __name__ == "__main__":
print(time())
datetime库:
轻松处理日期与时间:Python 的 Datetime 库指南_python datetime-CSDN博客
# 返回当前时间
import datetime as dt
if __name__ == '__main__':
print(dt.datetime.now())# 2024-06-07 09:13:21.105325
print(dt.datetime.now().year)# 2024
1.6.2 json
详解Python中的json库_python json库-CSDN博客
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,用于在不同应用程序之间传递数据。
json对象和python中的字典长得差不多,但前者是一种数据格式,而后者是一种数据结构。
具体来讲,json对象由一个大括号 {} 组成,大括号里保存的是若干个key-value对,其中key必须是字符串,且必须由双引号括起来(python的字典可以是单引号)。
#json.dumps 将 Python 对象编码成 JSON 字符串
#json.loads 将已编码的 JSON 字符串解码为 Python 对象
import json
if __name__ == '__main__':
obj = {'key': 'value'}
print(json.dumps(obj))
1.6.3path
关于路径的库
#os.path.abspath(path)-->返回路径path的绝对路径
# . 表示当前目录
from os import path
if __name__ == '__main__':
print(path.abspath('.'))
1.6.4math库
一些数学函数
Python中的math库_python math-CSDN博客
import math
# math.ceil向上取整
print(math.ceil(1.3333))
# math.factorial(x) 方法返回 x 的阶乘。
print(math.factorial(4))
# math.exp(x) 返回 e 的 x 次幂,Ex, 其中 e = 2.718281... 是自然对数的基数
print(math.exp(1))
# math.pow(x, y) 将返回 x 的 y 次幂。
print(math.pow(2,2))
# 等价于 x ** y
# math.sqrt(x) 返回 x 的平方根
print(math.sqrt(3))
1.6.5random随机数
探索Python中的随机数:random库的强大之处-CSDN博客
import random
# 生成指定区间内随机数
print(random.randint(0, 100))
1.6.6 hashlib哈希编码
Python的哈希hashlib模块详细解读_python_脚本之家
什么是哈希(Hash)? 哈希,是把任意长度的输入通过散列算法变换成固定长度的输出,简单的说就是通过
函数,将明文数据通过变成密文数据达到加密的作用。
示例:基于 sha256 的文件分片 hash 计算方式
import hashlib
def file_piece_sha256(in_file_path, piece_size):
sha256 = hashlib.sha256()# 构造一个hashlib的sha256对象,sha256是一种加密算法
# 二进制读的方式打开文件
with open(in_file_path, "rb") as in_file:
while True:
# 每次读piece_size个字符
piece = in_file.read(piece_size)
# 非空,不全为空格
if piece:
# 将字符串加载到sha256对象中,update对指定字符串进行加密
# hex()函数是Python内置函数之一,用于将整数转换为十六进制字符串
# encode函数用于将字符串按照指定的编码方式进行编码
sha256.update(piece.hex().encode('utf-8'))
else:
break
# hash.digest() —返回字节串对象
# 返回当前已传给 update() 方法的数据摘要。 这是一个大小为 digest_size 的字节串对象。
# 也就是说返回被加密后的字节串对象。
return sha256.digest().hex()
if __name__ == '__main__':
ret = file_piece_sha256('file_piece_sha256.py', 128)
print("file hash is: ", ret)
1.6.7numpy
1.7函数
1.7.1函数参数
实参分类和形参分类不冲突,即默认值参数在传递实参时可以写出参数名赋值(即关键字参数)
def Int(a=0):
print(a)
Int()
Int(1)
Int(a=2)
1.7.1.1按实参分类:位置参数和关键字参数
1)位置参数和关键字参数
def func(a,b,c):
print(a)
print(b)
print(c)
# 11是位置参数,c和b是关键字参数
# 传参的时候先写位置参数,再写命名参数
add_num(11,c=99,b=33)
#运行结果
#11
#33
#99
1.7.1.2按形参分类:必备参数、默认参数、不定长参数
1)必备参数和默认参数
# 默认参数一定要位于参数列表的最后面。
def func(a,b,c=99):
print(a)
print(b)
print(c)
func(11,22,33)
print('-----------')
# 默认参数调用函数的时候可以传可以不传,不传就用默认值
func(55,66)
#运行结果:
#11
#22
#33
#---------------
#55
#66
#99
2)不定长参数*args和 **kwargs
使用*args
和**kwargs
是Python的习惯写法,当然也可以用其他参数名,但最好使用习惯用法。
*args接收多传入的位置参数,以元祖的形式保存
# args接收的是一个元祖
def func(*args):
print(args)
func(33,44,55,66,77)
# 也可以可以先组装list或tuple,再通过*拆包逐个传入
func(*(33,44,55,66,77))
#运行结果
(33,44,55,66,77)
(33,44,55,66,77)
**kwargs接收多传入的关键字参数,以字典的形式保存
# kw接收的是一个字典;
def func(**kwargs):
print(kwargs)
func(e=33,h=44,f=55,d=66,c=77)
# 字典通过**解包
func(**{'e':33,'h':44,'d':66,'c':77})
#运行结果
{'e': 33, 'h': 44, 'f': 55, 'd': 66, 'c': 77}
{'e': 33, 'h': 44, 'f': 55, 'd': 66, 'c': 77}
1.8其他
1.8.1true和false
python中的True和False以及内置函数any()、all()_python ture-false-CSDN博客
非空的字典、列表、字符串0都是true!
1.8.2导入包的路径
绝对导入可以使用 import <> 或 from <> import <> 语法,但相对导入只能使用from <> import <> 形式;
Python中的相对导入是相对于当前包的路径进行导入,使用单个句点.
表示当前包,..
表示当前包的父级包。
relative_packages = '''
package/
__init__.py
subpackage1/
__init__.py
moduleX.py
moduleY.py
subpackage2/
__init__.py
moduleZ.py
moduleA.py
'''
#在文件package/subpackage1/moduleX.py里导入模块
from ..moduleA import foo
from . import moduleY
from .moduleY import spam as ham
from ..subpackage2.moduleZ import eggs
import ..subpackage2.moduleZ #错误,直接import不能用相对路径,
#可以绝对路径
import package.subpackage2.moduleZ
1.8.3__init__.py
Python 的包有两种形式,分别是Regular packages
和 namespace packages
。
所谓 Regular packages
就是指含有__init__.py的目录,这样的包被其他模块导入的时候,会先执行目录下__init__.py
里的代码。Regular packages
可以嵌套,也就是目录下的子目录也可以是一个包。
而 namespace packages
也是有层次结构的模块组织,不过它未必是存在文件夹里的,可以存在Zip压缩包里,也可以存在网络上,而且子包和父包也未必要存储在同一个地方。
如果一个模块被import
过,Python 会将导入的模块缓存在sys.modules
字典里缓存起来,再次导入的时候不会重新执行导入动作,直接从缓存里取。反之,如果我们从sys.modules
里把导入的模块删除,则再次import
会再次触发模块导入动作。
1.8.4 * 和**的用法:解包
掌握Python的解包技巧:*和**的最全用法_python dict用*解包-CSDN博客
示例:解包字典
info = {"name": "Alice", "age": 30}
person_info(**info) # 传递字典作为关键字参数
# 输出:
# Name: Alice
# Age: 30
1.8.5enumerate函数
python 内置函数——enumerate( )函数_enumerate函数python-CSDN博客
用来将一个可迭代对象转化为枚举对象,利用它可以同时获得每个元素的索引下标和值,即需要 index 和 value 值的时候可以使用enumerate函数。
l1 = ["小米", "小华", "小刘"]
for index, value in enumerate(l1):
print("%d,%s" % (index, value))
# 结果:
0,小米
1,小华
2,小刘
1.8.6增加缓存
https://blog.51cto.com/u_16213422/9270260
from cachetools import Cache
from cachetools import cached
cache = Cache(maxsize=100) #设置缓存最大大小为100
def check_cache(data):
# 在缓存中查找并返回所需数据
return cache.get(data)
def calculate(data):
# 计算所需数据
result = data * 2
return result
result = calculate(5)
print(result)
示例:缓存实现斐波那契数列
def fibonacci_inner(n, cache):
if n == 1 or n == 2:
return 1
r = 0
if cache.get(n) is None:
cache[n - 2] = fibonacci_inner(n - 2, cache)
cache[n - 1] = fibonacci_inner(n - 1, cache)
cache[n] = cache[n - 1] + cache[n - 2]
return cache[n]
def fibonacci(n):
return fibonacci_inner(n, {})
if __name__ == '__main__':
print(fibonacci(6))
1.8.7@修饰符
https://zhuanlan.zhihu.com/p/488609246
修饰符@的作用是为现有函数增加额外的功能,常用于插入日志、性能测试、事务处理等等。
def log(func):
def wrapper():
print('log开始 ...')
func()
print('log结束 ...')
return wrapper
@log
def test():
print('test ..')
test()
# 输出:
# log开始 ...
# test ..
# log结束 ...
1.8.8 format
python中.format()方法使用详解_python_脚本之家
用于填充字符串
text1 = 'hello'
text2 = 'world'
print("{}{}".format(text1, text2))
1.8.9try except
Python教学 | 有备无患!详解 Python 异常处理(try-except)_try except函数-CSDN博客
try:
<代码块1>
except:
<代码块2>
# 如果<代码块1>中的代码能够正常运行,那么程序就会逃过<代码块2>去执行后续的其他代码
# 但如果<代码块1>中的代码出现异常,那么程序就会执行<代码块2>中的代码,而不是直接报错和终止程序。
1.8.10assert
Python断言(assert)_python assert-CSDN博客
assert 1==1 #正常运行
assert 1==1 #表达式返回false,触发异常
1.8.11a,b=b,a交换值
Python心法:a,b=b,a原理_a,b=b,a是什么意思-CSDN博客
这行代码的结果是交换了a,b的值
1.8.12三元表达式
even_count += 2 if i % 4 == 0 else 1 if i % 2 == 0 else 0
1.8.13with as
Python 进阶语法:with...as..._pythonwithas语句-CSDN博客
常用于文件操作,能够自动关闭文档!
if __name__ == '__main__':
with open('/tmp/test.txt', 'w') as f:
f.write("test")
with open('/tmp/test.txt', 'r') as f:
print(f.read())
如果不用with as,对文档进行操作的话,一定要关闭文档!!!
if __name__ == '__main__':
# 打开文件进行写入
f = open('/tmp/test.txt', 'w')
f.write("test")
f.close() # 这行是新添加的,它将关闭之前打开的文件
# 再打开文件进行读取
f = open('/tmp/test.txt', 'r')
print(f.read())
f.close() # 再次关闭打开的文件
示例:计算耗时、__enter__和__exit__函数
关于__enter__和__exit__函数详见:
https://zhuanlan.zhihu.com/p/473007925
1.8.14 //整数除法向下取整
Python
中是没有 ++
和 --
的。
print(3//2)# 1
# 等价于
print(int(3//2))# 1
自增自减用
a=a+1
a=a-1
1.9迭代器
Python 中的 iter() 函数:迭代器的生成工具_python iter-CSDN博客
1.9.1iter()
my_tuple = (1, 2, 3, 4, 5)
# iter() 函数将元组 my_tuple 转换为一个迭代器对象 my_iterator,
# 然后使用 for 循环逐个访问迭代器中的元素,直到迭代结束。
# 注意my_iterator不是指针,而是一个可迭代对象,包含所有数据,因此可用于循环判断!!!!
my_iterator = iter(my_tuple)
# 当遍历完my_iterator中所有元素后结束循环
for item in my_iterator:
print(item)
my_list = [1, 2, 3, 4, 5]
my_iterator = iter(my_list)
while True:
try:
# 使用 next() 函数手动迭代,获取下一个对象
item = next(my_iterator)
print(item)
# 使用 StopIteration 异常来控制迭代的结束条件。
except StopIteration:
break
1.10 内置函数
1.10.1abs()绝对值
print(abs(-10)) # 输出:10
1.10. 2round()四舍五入
Python中round函数_python round函数-CSDN博客
result = round(3.14159)
print(result) # 输出: 3
1.10. 3all和any 全真则真和全假则假
Python基础_any()和all()_python中any和all-CSDN博客
numbers = [0, 1, 2, 3, 4]
# 使用 any() 测试是否存在正数
print(any(number > 0 for number in numbers))
# 使用 any() 测试是否存在负数
print(any(number < 0 for number in numbers))
# 执行结果
True
False
1.10.4bool()转换成布尔值
Python内置函数bool()详解 基本用法 特殊情况 总结——《跟老吕学Python编程》_bool函数python-CSDN博客
print(bool()) # 输出: False
1.10.5type()查看变量或对象的数据类型
Python的type()函数_pythontype()函数-CSDN博客
num = 42
print(type(num)) # 输出:<class 'int'>
1.10.6hash()计算对象的哈希值
number_hash = hash(42)
print(number_hash) # 输出:42
1.10.7dir()用于返回一个对象的所有属性和方法的列表
Python中的 dir() 函数示例详解_python_脚本之家
print(dir(math))
1.10.8ascci() 返回表示对象的 ASCII 字符串
Python 中的 ascii() 函数详解_python ascii-CSDN博客
print(ascii('你好')) # 输出: '\u4f60\u597d'
1.10.9bin()将整数转换为二进制字符串
Python内置函数bin()详解 使用方法 工作原理 返回值 示例 注意事项 实际应用——《跟老吕学Python编程》_bin()函数-CSDN博客
print(bin(0)) # 输出 '0b0'
1.10.10bytes()用于创建一个新的“bytes”对象
Python内置函数bytes()详解 bytes()函数的基本用法 bytes()函数与编码 注意事项 总结——《跟老吕学Python编程》_python bytes()函数-CSDN博客
b = bytes()
print(b) # 输出:b''
1.10.11chr()接受一个整数(范围在0-255之间)作为参数,并返回与该整数对应的ASCII字符
print(chr(72)) # 输出: 'H'
1.10.12hex()将整数转换为十六进制字符串
num = 255
hex_str = hex(num)
print(hex_str) # 输出:'0xff'
1.10.13str()将给定对象转换为字符串
num = 123
num_str = str(num)
print(num_str) # Output: '123'
1.10.14zip()
zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
a = [1,2,3]
b = [4,5,6]
c = [4,5,6,7,8]
zipped = zip(a,b) # 返回一个对象
print(list(zipped)) # list() 转换为列表
# [(1, 4), (2, 5), (3, 6)]
print(list(zip(a,c))) # 元素个数与最短的列表一致
#[(1, 4), (2, 5), (3, 6)]
1.10.15map()对列表应用某函数
Python之map()函数详解_python map-CSDN博客
seasons = ['Spring', 'Summer', 'Fall', 'Winter']
# lambda s: s[0]取首字母
shortcut = list(map(lambda s: s[0], seasons))
print(shortcut)
# ['S', 'S', 'F', 'W']
1.10.16filter()过滤函数
Python 过滤函数filter()详解_python filter函数-CSDN博客
seasons = ['Spring', 'Summer', 'Fall', 'Winter', 'sunshine']
non_s_seasons = list(filter(lambda s: s[0] != 'S', seasons))
print(non_s_seasons)
# ['Fall', 'Winter', 'sunshine']
s_seasons = list(filter(lambda s: s.lower()[0] == 'S', seasons))
print(s_seasons)
# []
1.10.17 encode()编码函数
Python编码解码之encode()函数详解_python_脚本之家
encode() 可以对字符串进行「编码」,常用来对「中文」字符串进行编码,以解决「乱码」问题。
print('hello'.encode('utf8'))
- ASCLL:美国早期制定的编码规范,只能表示128个字符。
- GB2312:中国在ASCLL基础上对中文进行扩展,可以表示6763个汉字符号。
- GBK:对GB2312的进一步扩展(K指扩),收录了21886个汉字符号。
- GB18030:对GBK再一次扩充,收录了70244个汉字符号。
- Unicode:字符集,包含了世界上目前所有的符号,全球的所有文字都可以解析,字符是定长的,统一为16位
- UTF-8:使用最广的一种Unicode的实现方式,每次8个位传输数据;体积太大,国内通常使用GBK。
- UTF-16:Unicode的另一种实现方式,每次传输16位数据
1.10.18 decode()解码函数
https://zhuanlan.zhihu.com/p/655784677
str = '伊斯坦布尔奇迹'
byte = str.encode('GBK')
# 如果编码不是使用的utf-8,则解码时要选择和编码时一样的格式,否则将出现报错
end_str = byte.decode('GBK')
print(end_str)
1.10.19 range()左闭右开
python中的range函数|python中的range函数|range()函数详解|Python中range(len())的用法_python range函数-CSDN博客
range(5)
#range(0, 5)
# 0 1 2 3 4
range(0, 10, 2)
# 0 2 4 6 8
range(5, 2, -1)
# 5 4 3
s = ["h","e","l","l","o"]
s[:] = [s[i] for i in range(len(s) - 1, -1, -1)]
# ["o","l","l","e","h"]
1.10.20 sorted排序算法
Python排序利器:深入理解sorted()函数_python的sorted函数怎么用-CSDN博客
numbers = [4, 1, 3, 2]
sorted_numbers = sorted(numbers, key=lambda x: -x) # 降序排序
print(sorted_numbers) # 输出:[4, 3, 2, 1]
1.10.21 ord()获取ASCII码或unicode编码
print(ord('a'))#97
1.10.22 del()删除
https://zhuanlan.zhihu.com/p/576128947
# 删除一整个字典
del 字典名
# 删除键值对
del 字典名[键名]
1.10.23 divmod()获取商和余数
n, r = divmod(236, 10)
#n=23,r=6
1.10.24 count()字符串和列表计数
python学习之Python count()函数详解_count函数python-CSDN博客
fruits = ['apple', 'banana', 'orange', 'apple', 'grape', 'apple']
count = fruits.count('apple')
print(count)
# 3
1.10.25 reversed()反转序列
一文带你掌握Python内置reversed函数的使用_python_脚本之家
s = ["h","e","l","l","o"]
s[:] = reversed(s)
# ["o","l","l","e","h"]
1.10.26 isdigit() 「判断」字符串是否只包含「数字」
Python isdigit()函数使用详解_isdigit函数怎么用-CSDN博客
print('123'.isdigit())
# True
2 进阶语法
2.1类
2.1.1基本用法
2.1.1.1self
Python类的函数第一个参数self:
- self只有在类的方法中才会有,独立的函数或方法是不必带有self的。self在定义类的方法时是必须有的,虽然在调用时不必传入相应的参数。
- self指的是类实例对象本身(注意:不是类本身)。
- self可以理解为一个字典变量,内部存的就是对象的数据属性。如:{‘name’:‘zhang’,‘age’:‘18’}就是这些。
# -> None 表示函数的返回值为空,也有-> int等用法
def __init__(self, text) -> None:
2.1.1.2
示例:密码城邦
涉及字符串拆分、类函数和对象调用
class CryptographyPeople:
def __init__(self, name_cn, name_en, role, desc):
self.name_cn = name_cn
self.name_en = name_en
self.role = role
self.desc = desc
class SimpleCryptographyPeopleParser:
def __init__(self, text) -> None:
self.text = text
def parse(self, desc):
# 解析名字部分
name_cn, name_en, rest = self.parse_name(desc)
# 解析角色部分
role, rest = self.parse_role(rest)
# 解析描述不符
desc = self.parse_desc(name_cn, name_en, role, rest)
# 创建密码城邦人物
people = CryptographyPeople(name_cn, name_en, role, desc)
# people就有了那些属性,可以用.调用
return people
def parse_name(self, text):
# 解析名字部分
index = text.find('是')
name, rest = text[0:index], text[index+1:]
# 解析中英文名字
start = name.find('(')
end = name.find(')')
name_cn = name[0:start]
name_en = name[start+1:end]
return name_cn.strip(), name_en.strip(), rest
def parse_role(self, text):
index1 = text.find('。')
index2 = text.find(',')
index = 0 # 找名字后面紧跟着的第一个身份描述,在第一个标点符号之前
if index1 > 0 and index2 > 0:
index = min(index1, index2)
else:
index = max(index1, index2)
role, rest = text[0:index], text[index+1:len(text)-1]
# 去除冗余量词
counts = ['一名', '一位', '一个']
for count in counts:
# .replace()将量词替换成'',相当于去掉了量词
role = role.replace(count, '')
return role.strip(), rest.strip()
def parse_desc(self, name_cn, name_en, role, rest):
desc = rest
if desc:
# 识别自我主语
self_list = [name_cn, '他', '她']
for self_item in self_list:
desc = desc.replace(self_item, '我')
else:
# 补充默认描述
desc = '很高兴认识你'
return desc
class CryptographyCity:
def __init__(self):
self.peoples = []
def add(self, text):
parser = SimpleCryptographyPeopleParser(text)
people = parser.parse(text)
self.peoples.append(people)
def introduce(self):
for people in self.peoples:
self.say(people)
def say(self, people):
info = f'{people.name_cn}({people.name_en}): 密码学家说我是一位{people.role},{people.desc}。'
print(info)
if __name__ == '__main__':
crypto_roles = [
'爱丽丝(Alice)是信息发送者。',
'与鲍伯(Bob)是信息接受者。通例上,爱丽丝希望把一条消息发送给鲍伯。',
'卡罗尔或查利(Carol或Charlie)是通信中的第三位参加者。',
'戴夫(Dave)是通信中的第四位参加者。',
'伊夫(Eve)是一位偷听者(eavesdropper),但行为通常是被动的。她拥有偷听的技术,但不会中途篡改发送的消息。在量子密码学中,伊夫也可以指环境(environment)。'
]
city = CryptographyCity()
for crypto_role in crypto_roles:
city.add(crypto_role)
city.introduce()
2.1.1.3__init__(self)->None
class HashKeyValueSet(KeyValueSet):# 继承
def __init__(self) -> None:# None说明没有返回值
# super().__init__() 就是调用父类的__init__()方法
super().__init__()
2.1.1. 4super()
super().__init__()函数_super.init-CSDN博客
super()用来调用父类(基类)的方法,__init__()
是类的构造方法,super().__init__()
就是调用父类的__init__()
方法, 同样可以使用super()去调用父类的其他方法。
class Parent:
def __init__(self, name):
self.name = name
class Child(Parent):# 继承
def __init__(self, name, age):
super().__init__(name) # 调用父类的构造方法以初始化 name
self.age = age #添加自定义age属性
child = Child("Alice", 25)
print(child.name) # 输出 "Alice"
print(child.age) # 输出 25
2.1.2继承和组合
Python 类的继承和组合_python 中的继承和组合-CSDN博客
https://www.cnblogs.com/hantaozi430/articles/8477832.html
组合:当前类的某一属性是另一个类的实例,可以调用该实例的类函数等
也就是,把类B赋值给类A的某一个属性(如年纪),A的对象就可以通过年纪调用B的函数和属性?调用方法:A的某一个对象.年纪.B的类函数()
也可以只把类B的某个函数赋值给类A的某一个属性
继承:
class ParentClass1: #定义父类
pass
class ParentClass2: #定义父类
pass
class SubClass1(ParentClass1): #单继承,基类是ParentClass1,派生类是SubClass
pass
class SubClass2(ParentClass1,ParentClass2): #python支持多继承,用逗号分隔开多个继承的类
pass
组合:
class Skill:
def fire(self):
print("release Fire skill")
class Riven:
camp='Noxus'
def __init__(self,nickname):
self.nickname=nickname
self.skill5=Skill().fire()#Skill类产生一个对象,并调用fire()方法,赋值给实例的skill5属性
r1=Riven("瑞雯")
2.2文件
2.2.1read()读文件
详细分析Python中的read()、readline、readlines()方法_python .read()-CSDN博客
read()
函数:从文件中读取指定数量的字节,默认情况下会读取整个文件
with open('example.txt', 'r') as file:
content = file.read()
print(content)
with open('example.txt', 'r') as file:
# read的参数指定读几个字符
partial_content = file.read(10)
print(partial_content) # 输出前十个字符
2.2. 2write()写文件
Python写入文件的两种方法(write/writelines)举例解析_python writelines-CSDN博客
# 以utf-8编码格式打开文件,少数情况下添加或者缺少该代码会出现报错或识别乱码
f=open(r'C:\Users\Administrator\Desktop\1.txt','w',encoding='utf-8')
print(f.write('霜天'))
f.close()
2.2.3 json文件
【Python基础】Python中处理JSON文件的全面指南_python json-CSDN博客
使用Python读取json文件的方法小结_python_脚本之家
【Python】json.dumps()函数详解和示例-CSDN博客
示例:json文件读写
import json
def load_json(file):
with open(file, 'r') as f:# f是一个文件类对象
# read()将f转换成字符串
# json.loads()用于将str类型的数据转成dict。
return json.loads(f.read())
def dump_json(file, obj):
with open(file, 'w') as f:
# 写入json文件的时候,要指定ensure_ascii参数为False,
# 否则中文编码虽然为utf_8,但仍然无法显示中文,而是\uxxx形式的编码。
# json.dumps()用于将dict类型的数据转成str,因为如果直接将dict类型的数据写入json文件中会发生报错,因此在将数据写入时需要用到该函数。
# indent:用于设置输出的 JSON 字符串的缩进层次,使其更易读。
f.write(json.dumps(obj, indent=2, ensure_ascii=False))
if __name__ == '__main__':
data = {
'test': 1,
}
dump_json('test.json', data)
load_json('test.json')
2.2.3.1load和loads的区别、dump 和 dumps的区别
Python教程:json中load和loads的区别_json的loads和load-CSDN博客
- dump 和 dumps 都实现了序列化(python对象到json格式)
- load 和 loads 都实现反序列化(json格式到python对象)
根据序列化和反序列的特性,load和dump针对文件操作,带s的针对内存对象操作
- loads: 是将string转换为dict
- dumps: 是将dict转换为string
- load: 是将里json格式字符串转化为dict,读取文件
- dump: 是将dict类型转换为json格式字符串,存入文件
2.2.4 open()
https://zhuanlan.zhihu.com/p/589237868
四种基本 mode:
'r'
:只读。默认值。如果文件不存在,抛出 FileNotFoundError 异常。
'w'
:创建文件。如果文件已存在,则清空该文件。
'x'
:创建文件。如果文件已存在,抛出 FileExistsError 异常。
'a'
:创建文件。如果文件已存在,则在文件末尾追加内容。
以下两种 mode 需要与四种基本 mode 一起使用:
't'
:以本文(text)模式读写。默认值(r 和 rt 完全相同,w 和 wt 完全相同,等等)。
'b'
:以二进制(binary)模式读写。对于非文本文件(图片、音频等),或想复制移动文件,可以使用 b 模式。
with open('test.txt', mode='w') as f:
with open('test.txt', 'w') as f:
2.2.5os.path用于文件的属性获取
2.2.5.1os.path.exists判断文件或文件夹是否存在
详解python os.path.exists判断文件或文件夹是否存在_python_脚本之家
如果存在,则返回True
;如果不存在,则返回False
。
import os.path
file_path = '/path/to/file.txt'
if os.path.exists(file_path):
print(f"{file_path} 存在")
else:
print(f"{file_path} 不存在")
2.2.5.2 __file__
python 中__file__代表什么 怎么使用-CSDN博客
__file__是Python中内置的变量,它表示当前文件的文件名。
importos
print(os.path.abspath(__file__))# 输出当前文件绝对路径
2.3其他
2.3.1异常
示例:出现异常正常打印日志和堆栈
日志介绍:
Python日志库logging总结-可能是目前为止将logging库总结的最好的一篇文章_python logging-CSDN博客
import json
import traceback
import logging
# __name__ 是 Python 中的一个特殊内置变量,他代表当前模块的名称(默认为 __main__)
logger = logging.getLogger(__name__)
def load_json(file):
with open(file, 'r') as f:
return json.loads(f.read())
def test():
try:
ret = load_json('a.json')
return {'err': 'success', 'result': ret}
except Exception as e: # 捕获异常
# logging.error('This is an error message')打印error信息
logger.error(f"load json exception:{str(e)}")
# traceback.format_exc()来获取完整的堆栈信息,然后使用logger.error进行记录。
logger.error(traceback.format_exc())
return {'err': 'exception'}
if __name__ == '__main__':
test()
2.4正则表达式
2.4.1 re.match()
import re
#re.match(pattern, string, flags=0)
#pattern 匹配的正则表达式
#string 要匹配的字符串。
#flags 标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等。
# span() 是 re 模块中的一个函数,它用于在字符串中搜索模式并返回匹配的起始和结束位置。
print(re.match('www', 'www.runoob.com').span()) # 在起始位置匹配
print(re.match('com', 'www.runoob.com')) # 不在起始位置匹配
#输出:
#(0, 3)
#None
2.4.2group()
使用 group(num) 或 groups() 匹配对象函数来获取匹配表达式。
import re
text = "abc123def456"
pattern = "(\d+)(\w+)"
match = re.search(pattern, text)
# 获取第一组匹配结果,即第一个()的匹配结果
print(match.group(1))# 123
print(match.group(2))# def456
示例:简单计算器
import re
def naive_calc(code):
# 用换行符分割,并去除第一行最后一行什么都没有的
code_lines = [l for l in code.split('\n') if l.strip() != '']
for line in code_lines:
# [\+\-\*\/] 匹配加减乘除任一个
# \d+ 匹配数字,至少一个
# \s* 匹配空白字符(空格、制表符等),0或多个
# () 中的为整体被匹配和获取结果
ret = re.match("\s*(\d+)([\+\-\*\/])(\d+)\s*", line)
# 返回指定分组的匹配结果
left = ret.group(1)
op = ret.group(2)
right = ret.group(3)
if op == '+':
print('{}+{}={}'.format(left, right, int(left)+int(right)))
elif op == '-':
print('{}-{}={}'.format(left, right, int(left)-int(right)))
elif op == '*':
print('{}*{}={}'.format(left, right, int(left)*int(right)))
elif op == '/' and right != '0':
print('{}/{}={}'.format(left, right, int(left)/int(right)))
def test():
code = '''
1+2
3+4
5-3
4*3
10/2
'''
naive_calc(code)
if __name__ == '__main__':
test()
2.5 sys
2.5.1 sys.argv获取命令行参数
# python脚本名为Demo.py
import sys
# sys.argv[0]通常就是指该Python程序脚本名称,
# sys.argv[1]代表第一个参数,sys.argv[2]代表第二个参数,第三、第四参数以此类推
print(sys.argv)# 打印参数列表
print(sys.argv[0])
if sys.argv[1] == 'A':
print('我是A命令,该执行A命令了')
elif sys.argv[1] == 'B':
print('我是B命令,该执行B命令了')
else:
print('我是C命令,该执行C命令了')
3 库
3.1 Numpy
numpy数组与python列表的区别
1、python列表可以存放不同数据类型,numpy数组只能是相同数据类型(一般情况下)
2、数组是可以多维的,相当于线性代数的矩阵
数组中的元素在内存中是连续存储的。
3.1.1 array数组
python中的数组(Array)_python数组-CSDN博客
3.1.1.1导入NumPy库
import numpy as np
3.1.1.2 创建、访问、修改
import numpy as np
arr = np.array([1, 2, 3, 4, 5]) # 创建一维数组
print(arr) # 输出: [1 2 3 4 5]
matrix = np.array([[1, 2, 3], [4, 5, 6]]) # 创建二维数组
print(matrix)
# 输出:
# [[1 2 3]
# [4 5 6]]
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[0]) # 输出: 1,访问第一个元素
arr[2] = 10 # 修改第三个元素为10
print(arr) # 输出: [ 1 2 10 4 5]
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix[0, 1]) # 输出: 2,访问第一行第二列元素
matrix[1, 2] = 7 # 修改第二行第三列元素为7
print(matrix)
# 输出:
# [[1 2 3]
# [4 5 7]]
import numpy as np
if __name__ == '__main__':
shape = (3, 3)
print('创建0数组,元素全部为0,用dtype参数指定数据类型')
print(np.zeros(shape, dtype=int))
print('创建元素全为1的数组,用dtype指定数据类型')
print(np.ones(shape, dtype=int))
print('创建范围序列,用dtype指定数据类型')
print(np.arange(2, 10, dtype=float))
# np.arange函数用于创建一维数组,它可以生成一个等差数列。
# 该函数的语法为: np.arange(start, stop, step)
# 其中start表示起始值,stop表示终止值(不包含) , step表示步长。
# 例如,np.arange(0, 10, 2)可以生成一个从0开始,步长为2,不包含10的一维数组[0, 2, 4, 6, 8]。
print('创建未初始化的数组')
print(np.random(shape, dtype=int))
——————————————————————————————————————————
创建0数组,元素全部为0,用dtype参数指定数据类型
[[0 0 0]
[0 0 0]
[0 0 0]]
创建元素全为1的数组,用dtype指定数据类型
[[1 1 1]
[1 1 1]
[1 1 1]]
创建范围序列,用dtype指定数据类型
[2. 3. 4. 5. 6. 7. 8. 9.]
创建未初始化的数组
import numpy as np
if __name__ == '__main__':
examples = np.array([
[
"创建单位矩阵",
"创建对角矩阵",
"创建范德蒙矩阵"
],
[
lambda: np.eye(3),
lambda: np.diag([1, 2, 3]),
lambda: np.vander(np.linspace(0, 2, 5), 2)
]
])
for i in range(examples.shape[1]):
tip = examples[0][i]
action = examples[1][i]
print(tip)
print(action())
___________________________________
创建单位矩阵
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
创建对角矩阵
[[1 0 0]
[0 2 0]
[0 0 3]]
创建范德蒙矩阵
[[0. 1. ]
[0.5 1. ]
[1. 1. ]
[1.5 1. ]
[2. 1. ]]
3.1.1.3ndarray对象的属性
属性 说明
.shape ndarray对象维度的元组
.ndim ndarray对象的维度
.size ndarray对象中元素总数,相当于.shape中n*m的值
.dtype ndarray对象的元素类型
.itemsize ndarray对象中每个元素的大小,以字节为单位
.flags 这个函数返回了它们的当前值
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
shape = matrix.shape
print(shape) # 输出: (2, 3),表示2行3列的二维数组
import numpy as np
arr = np.array([5, 2, 1, 6, 4])
maximum = np.max(arr) # 计算数组的最大值
print(maximum) # 输出: 6
minimum = np.min(arr) # 计算数组的最小值
print(minimum) # 输出: 1
mean = np.mean(arr) # 计算数组的平均值
print(mean) # 输出: 3.6
sorted_arr = np.sort(arr) # 对数组进行排序
print(sorted_arr) # 输出: [1 2 4 5 6]
import numpy as np
arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
subset = arr[2:6] # 获取索引2到5(不包括6)的子集
print(subset) # 输出: [2 3 4 5]
reversed_arr = arr[::-1] # 将数组逆序
print(reversed_arr) # 输出: [9 8 7 6 5 4 3 2 1 0]
3.1.1.3其他属性
3.1.1.3.1np.argsort()
函数功能:将a中的元素从小到大排列,提取其在排列前对应的index(索引)输出。
import numpy as np
x=np.array([1,4,3,-1,6,9])
y=np.argsort(x)
print('一维数组的排序结果:{}'.format(y))
# 一维数组的排序结果:[3 0 2 1 4 5]
3.1.1.3.2 np.random()
Numpy的np.random随机模块详解_python_脚本之家
【Numpy】一文向您详细介绍 np.random.choice()-CSDN博客
import numpy as np
print('创建未初始化的数组')
shape = (3, 3)
print(np.random(shape, dtype=int))
# np.random.randint用于生成随机整数。
# np.random.randint(low, high=None, size=None, dtype='l')
# low:生成的随机整数的下限(包含)
# high:生成的随机整数的上限(不包含)
# size:生成数组的形状
# dtype:生成数组的数据类型
np.random.randint(0, 100, size=5)
# np.random.choice()函数还接受一些可选参数,用于控制随机选择的行为。
# size:输出数组的形状。例如,size=3将返回一个包含3个随机选择元素的数组。
# replace:是否允许重复选择。默认为True,即允许重复选择;如果设置为False,则进行无放回抽样。
# p:与输入数组形状相同的概率数组。它指定了从每个元素中选择的概率。
import numpy as np
arr = np.array(['a', 'b', 'c', 'd'])
# 从arr中随机选择3个元素,允许重复
choices_with_replacement = np.random.choice(arr, size=3, replace=True)
print(choices_with_replacement) # 输出可能是['a', 'b', 'c']、['a', 'a', 'd']等
# 从arr中随机选择3个元素,不允许重复(无放回抽样)
choices_without_replacement = np.random.choice(arr, size=3, replace=False)
print(choices_without_replacement) # 输出可能是['a', 'b', 'c']、['b', 'd', 'a']等,且不会有重复元素
# 指定每个元素被选择的概率
probabilities = np.array([0.1, 0.1, 0.8, 0])
choices_with_probabilities = np.random.choice(arr, size=3, p=probabilities, replace=True)
print(choices_with_probabilities) # 'c'被选中的概率最大,'d'不会被选中
3.1.1.3.3 np.astype()
x = np.array([1, 2, 2.5])
print(x.astype(int))
# [1 2 2]
b = np.array([-1, 0, 7], dtype=np.int32)
print(b.astype(np.bool))
# [ True False True]
3.1.1.3.4 加减乘除
import numpy as np
def operate_array(operate, x, y):
if operate == '+':
print('数组相加:')
res = x + y
print(res)
print('数组相加:numpy方法:')
res = np.add(x, y)
print(res)
elif operate == '-':
print('数组相减:')
res = x - y
print(res)
print('数组相减,numpy方法:')
res = np.subtract(x, y)
print(res)
elif operate == '*':
print('数组相乘,逐元素矩阵乘法:')
res = x * y
print(res)
print('数组相乘,numpy方法:逐元素矩阵乘法:')
res = np.multiply(x, y)
print(res)
elif operate == '/':
print('数组相除,对应元素相除:')
res = x / y
print(res)
print('数组相除,numpy方法:对应元素相除:')
res = np.divide(x, y)
print(res)
if __name__ == '__main__':
x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[4, 5, 6], [7, 8, 9]])
operate_array('+', x, y)
operate_array('-', x, y)
operate_array('*', x, y)
operate_array('/', x, y)
——————————————————————————————
数组相加:
[[ 5 7 9]
[11 13 15]]
数组相加:numpy方法:
[[ 5 7 9]
[11 13 15]]
数组相减:
[[-3 -3 -3]
[-3 -3 -3]]
数组相减,numpy方法:
[[-3 -3 -3]
[-3 -3 -3]]
数组相乘,逐元素矩阵乘法:
[[ 4 10 18]
[28 40 54]]
数组相乘,numpy方法:逐元素矩阵乘法:
[[ 4 10 18]
[28 40 54]]
数组相除,对应元素相除:
[[0.25 0.4 0.5 ]
[0.57142857 0.625 0.66666667]]
数组相除,numpy方法:对应元素相除:
[[0.25 0.4 0.5 ]
[0.57142857 0.625 0.66666667]]
进程已结束,退出代码0
3.1.1.3.5 求和、平均、标准差、方差、最小值、最大值、中位数找不同
import numpy as np
if __name__ == '__main__':
data = np.random.randint(0, 10, size=(10))
print('输入数据: {}'.format(data))
data_sum = np.sum(data)
print('求和: {}'.format(data_sum))
data_mean = np.mean(data)
print('求平均: {}'.format(data_mean))
data_std = np.std(data)
print('求标准差: {}'.format(data_std))
data_var = np.var(data)
print('求方差: {}'.format(data_var))
data_min = np.min(data)
print('求最小值: {}'.format(data_min))
data_max = np.max(data)
print('求最大值: {}'.format(data_max))
data_median = np.median(data)
print('求中位数: {}'.format(data_median))
data_unique = np.unique(data)
print('找出数组中所有不同的值,并按从小打大排序: {}'.format(data_unique))
————————————————————————————————————————————————
输入数据: [1 6 9 5 4 1 7 6 8 9]
求和: 56
求平均: 5.6
求标准差: 2.7640549922170505
求方差: 7.639999999999999
求最小值: 1
求最大值: 9
求中位数: 6.0
找出数组中所有不同的值,并按从小打大排序: [1 4 5 6 7 8 9]
3.1.2 掩码数组
https://www.cnblogs.com/zhangyafei/p/10567745.html
3.1.2.1基本概念
import numpy as np
import numpy.ma as ma
x = np.array([1, 2, 3, -99, 5])
print(x)# [ 1 2 3 -99 5]
# 创造一个掩码数组(标记第四个元素为无效状态)。
mx = ma.masked_array(x, mask=[0, 0, 0, 1, 0])
print(mx)# [1 2 3 -- 5]
# 可以计算平均值而不用考虑无效数据。
print(mx.mean())# 2.75
3.1.2.2 访问掩码
import numpy as np
import numpy.ma as ma
x = np.array([1, 2, 3, -99, 5])
print(x)# [ 1 2 3 -99 5]
# 创造一个掩码数组(标记第四个元素为无效状态)。
mx = ma.masked_array(x, mask=[0, 0, 0, 1, 0])
print(mx)# [1 2 3 -- 5]
# mask属性访问掩码数组的掩码。我们必须记住,掩码中的True条目表示无效数据。
print(mx.mask)# [False False False True False]
# 使用numpy.logical_not函数或简单使用~运算符计算掩码的逆
# 从而只访问有效数据
x = ma.array([[1, 2], [3, 4]], mask=[[0, 1], [1, 0]])
print(x[~x.mask])# [1 4]
# 使用compressed方法,该方法返回一维ndarray(或其子类之一,取决于baseclass属性)
print(x.compressed())# [1 4]
3.1.2.3 修改掩码
import numpy as np
import numpy.ma as ma
# 通过将True赋给掩码,可以立即屏蔽数组的所有数据:
x = ma.array([1, 2, 3], mask=[0, 0, 1])
x.mask = True
print(x)# [-- -- --]
# 通过向掩码分配一系列布尔值来对特定数据条目进行掩码和/或取消掩码:
x = ma.array([1, 2, 3])
x.mask = [0, 1, 0]
print(x)# [1 -- 3]
3.1.2.4 取消掩码
import numpy as np
import numpy.ma as ma
# 要取消屏蔽一个或多个特定数据条目,我们只需为它们分配一个或多个新的有效值
x = ma.array([1, 2, 3], mask=[0, 0, 1])
print(x)# [1 2 --]
# 将最后一个元素重新赋新的有效值
x[-1] = 5
print(x)# [1 2 5]
# 要取消屏蔽掩码数组的所有掩码条目(假设掩码不是硬掩码),最简单的解决方案是将常量nomask分配给掩码:
x = ma.array([1, 2, 3], mask=[0, 0, 1])
print(x)# [1 2 --]
x.mask = ma.nomask
print(x)# [1 2 3]
3.1.2.5索引和切片
由于MaskedArray
是numpy.ndarray
的子类,它会继承其用于索引和切片的机制。
注意:
表现形式:print()结果为 -- 打印type为numpy.ma.core.MaskedConstant
使用 if type(x) == np.ma.core.MaskedConstant: 即可挑选出该数据
当表现形式为:print()结果为nan 打印type为float时
使用 if np.isnan(y): 即可挑出该数据
import numpy as np
import numpy.ma as ma
x = ma.array([1, 2, 3], mask=[0, 0, 1])
print(x)# [1 2 --]
print(x[0])# 1
print(x[-1])# --
# 被掩码的数据的类型为numpy.ma.core.MaskedConstant
assert type(x[-1]) is np.ma.core.MaskedConstant
print(x[-1] is ma.masked)# True
x = ma.array([1, 2, 3, 4, 5], mask=[0, 1, 0, 0, 1])
mx = x[:3]
print(mx)# [1 -- 3]
mx[1] = -1
print(mx)# [1 -1 3]
# 切片改了原始数据也改了!!!!!!!!(列表的切片更改不会影响原数据,但掩码数组会!)
print(x.mask)# [False False False False True]
print(x.data)# [ 1 -1 3 4 5]
print(x)# [1 -1 3 4 --]
3.1.3 np.int
在Python中,int是内置的整数数据类型,而np.int是NumPy库中的数据类型。虽然它们都代表整数,但它们之间存在一些区别。
一、数据精度方面不同:Python中的int可以表示任意大小的整数,而np.int是有限的整数类型,它的精度是与计算机的一个字大小有关。例如,32位的系统中,np.int的范围是从-2,147,483,648到2,147,483,647,而64位的系统中,np.int的范围是从-9,223,372,036,854,775,808到9,223,372,036,854,775,807。
二、存储方式方面不同:Python中的int是动态分配的,大小是根据需要自动调整的,而np.int是固定大小的,它的内存大小是确定的。
三、类型转换方面不同:在Python中,可以将其他数据类型(如字符串)转换为int类型,但转换之前需要进行合法性校验;在NumPy中,不能将其他数据类型自动转换为np.int类型,需要使用astype()方法进行显式转换。
3.1.4 np.matrix
numpy的简单使用(三)_np.matrix-CSDN博客
a = np.random.randint(0, 10, size=(3, 3))
# 将二维数组转换成矩阵
matrix_b = np.matrix(a)
print(matrix_b)
print(matrix_b.T)# 转置
print(matrix_b.I)# 逆
————————————
[[6 5 5]
[5 4 0]
[6 0 8]]
[[6 5 6]
[5 4 0]
[5 0 8]]
[[-0.25 0.3125 0.15625 ]
[ 0.3125 -0.140625 -0.1953125]
[ 0.1875 -0.234375 0.0078125]]
4 算法
4.1 链表
示例:使用字典构造链表
def init():
return {"pre": None, "key": None, "value": None, "next": None}
def set(dict, key, value):
node = dict
while node['next'] is not None:
if node['key'] == key:
return
node = node['next']
node['next'] = {"pre": node, "key": key, "value": value, "next": None}
def get(dict, key):
node = dict
while node is not None:
if node['key'] == key:
return node['value']
node = node['next']
def exist(dict, key):
node = dict
while node is not None:
if node['key'] == key:
return True
node = node['next']
return False
if __name__ == '__main__':
a = init()
set(a, 'one', 1)
set(a, 'two', 2)
print(get(a, 'one'))
print(get(a, 'two'))
print(get(a, 'three'))
4.2 堆
4.2.1 heapq模块
https://www.cnblogs.com/mastershun/p/18000318
import heapq
lists = [3, 10, 20, 52, 2, 83, 52, 81, 51]
#一、 创建空列表,然后使用heappush将数据添加到空列表中,每添加一个新数据后,该列表都满足小顶堆特性。
heap = []
for i in lists:
heapq.heappush(heap, i)
print("lists: ",lists)
print("heap: ",heap)
# 二、使用heapify直接将数据列表调整为小顶堆格式
heapq.heapify(lists)
print("The lists after heap is : ",lists)
heapq模块学习笔记_heappush(heap,(value,key))用法-CSDN博客
q = []
# q是堆容器,堆的每个元素是一个元组(-nums[i], i)
# 建堆时根据第一个数-nums[i]为依据,进行堆排序
heapq.heappush(q, (-nums[i], i))