feat: 容器类型相关内容

This commit is contained in:
lichao 2024-12-09 11:47:37 +08:00
parent fce0ff43bb
commit faab42e00d
7 changed files with 194 additions and 5 deletions

25
.idea/watcherTasks.xml Normal file
View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectTasksOptions">
<TaskOptions isEnabled="true">
<option name="arguments" value="$FilePath$" />
<option name="checkSyntaxErrors" value="true" />
<option name="description" />
<option name="exitCodeBehavior" value="ERROR" />
<option name="fileExtension" value="py" />
<option name="immediateSync" value="true" />
<option name="name" value="black" />
<option name="output" value="$FilePath$" />
<option name="outputFilters">
<array />
</option>
<option name="outputFromStdout" value="false" />
<option name="program" value="$USER_HOME$/Library/Python/3.9/bin/black" />
<option name="runOnExternalChanges" value="true" />
<option name="scopeName" value="Project Files" />
<option name="trackOnlyRoot" value="false" />
<option name="workingDir" value="" />
<envs />
</TaskOptions>
</component>
</project>

22
basic_learn/元组.py Normal file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@File : 元组.py
@Author : lichao
@Date : 2024/11/27 17:21
@Software : PyCharm
"""
tuple_data = (1, 2, 3, 4, 5)
print(tuple_data)
# 元组不可变,不能修改,不能删除,不能添加
# tuple_data[0] = 2 # TypeError: 'tuple' object does not support item assignment
# 错误的推导式,会生成一个生成器
tuple_g_data = (i for i in range(1, 10))
print(tuple_g_data) # <generator object <genexpr> at 0x7f8f8f7b4d60>
# 可以使用tuple来生成元组
tuple_g1_data = tuple(i for i in range(1, 10))
print(tuple_g1_data)

View File

@ -9,19 +9,27 @@
# 创建列表的两种方式
# 1.使用[]创建
list1 = [1,2,3,4,5]
list1 = [1, 2, 3, 4, 5]
list1.append(6)
# 2.使用list()创建
list2 = list('hello')
list2 = list("hello")
print(list1,list2)
print(list1, list2)
# 3.使用+ 连接两个列表
# 3.使用 + 连接两个列表
list3 = list1 + list2
print(list3)
# 4.使用extend()方法,将一个列表中的元素添加到另一个列表中
list4 = [1, 2, 3]
list5 = [4, 5, 6]
list4.extend(list5)
print(list4)
# 使用 + 不改变原列表, 使用+= 更改原列表
# 使用append()改变原列表
insert_data = [1, 2, 4]
insert_data.insert(2, 3) # 在索引2的位置插入3

36
basic_learn/字典.py Normal file
View File

@ -0,0 +1,36 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@File : 字典.py
@Author : lichao
@Date : 2024/12/6 11:17
@Software : PyCharm
"""
dict_data = {"name": "test", "age": 18, "address": "beijing"}
# 直接对字典进行遍历获得是字典的key
for key in dict_data:
print(key)
# 元组拆包
for key, value in dict_data.items():
print(key, value)
# 获取字典数据,如果访问不存在的key会报错
print(dict_data["name"])
# 使用get方法如果访问不存在的key不会报错返回None,第二个参数用来设置默认值(建议使用)
# print(dict_data["height"])
print(dict_data.get("height", 180))
dict_data["name"] = "test1"
dict_data["gender"] = "man"
print(dict_data)
# 删除字典中的元素
del dict_data["age"]
# 清空字典
dict_data.clear()
# 空字典声明
dict_data1 = {}
dict_data2 = dict()

24
basic_learn/推导式.py Normal file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@File : 推导式.py
@Author : lichao
@Date : 2024/12/9 11:46
@Software : PyCharm
"""
# 列表推导式
list = [x for x in range(1, 15)]
list = [list[x : x + 3] for x in range(0, len(list), 3)]
print(list)
# 集合推导式
gather = {x for x in range(1, 10)}
print(gather) # {1, 2, 3, 4, 5, 6, 7, 8, 9}
# 字典推导式
dict = {x: x**2 for x in range(1, 10)}
print(dict)
# 没有元组推导式

50
basic_learn/集合.py Normal file
View File

@ -0,0 +1,50 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@File : 集合.py.py
@Author : lichao
@Date : 2024/12/6 10:38
@Software : PyCharm
"""
# 集合是一个无序的、不重复的数据集合,列表是有序的,可以重复的数据集合
# 创建集合的两种方式
# 1.使用{}创建
set_data = {1, 2, 3, 4, 5}
print(type(set_data))
# 集合不能使用下标访问
# print(set_data[0]) # TypeError: 'set' object is not subscriptable
# 不能使用{}创建空集合,因为{}创建的是空字典
# set_data1 = {} # <class 'dict'>
set_data2 = set()
print(type(set_data2))
# 集合方法
# 1.添加元素
set_data2.add(1)
# 2.删除元素
set_data2.remove(1)
# 3.清空集合
set_data2.clear()
# 4.删除集合
del set_data2
# 5.集合的交集、并集、差集、对称差集
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
# 交集
print(set1 & set2) # {4, 5}
print(set1.intersection(set2)) # {4, 5}
# 并集
print(set1 | set2) # {1, 2, 3, 4, 5, 6, 7, 8}
print(set1.union(set2)) # {1, 2, 3, 4, 5, 6, 7, 8}
# 差集
print(set1 - set2) # {1, 2, 3}
print(set1.difference(set2)) # {1, 2, 3}
# 对称差集
print(set1 ^ set2) # {1, 2, 3, 6, 7, 8}
print(set1.symmetric_difference(set2)) # {1, 2, 3, 6, 7, 8}

24
demo.py Normal file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@File : demo.py
@Author : lichao
@Date : 2024/11/27 15:40
@Software : PyCharm
"""
# 列表推导式
list = [x for x in range(1, 15)]
list = [list[x : x + 3] for x in range(0, len(list), 3)]
print(list)
# 集合推导式
gather = {x for x in range(1, 10)}
print(gather) # {1, 2, 3, 4, 5, 6, 7, 8, 9}
# 字典推导式
dict = {x: x**2 for x in range(1, 10)}
print(dict)
# 没有元组推导式