一:公共操作
-
运算符
-
公共⽅法
-
容器类型转换
运算符
运算符 | 描述 | 支持的容器类型 |
---|---|---|
+ | 合并 | 字符串,列表,元组 |
* | 复制 | 字符串,列表,元组 |
in | 是否存在 | 字符串,列表,元组,字典,集合 |
not in | 是否不存在 | 字符串,列表,元组,字典,集合 |
+合并
# 1. 字符串
str1 = 'aa'
str2 = 'bb'
str3 = str1 + str2
print(str3) # aabb
# 2. 列表
list1 = [1, 2]
list2 = [10, 20]
list3 = list1 + list2
print(list3) # [1, 2, 10, 20]
# 3. 元组
t1 = (1, 2)
t2 = (10, 20)
t3 = t1 + t2
print(t3) # (10, 20, 100, 200)
*复制
# 1. 字符串
print('-' * 10) # ----------
# 2. 列表
list1 = ['hello']
print(list1 * 4) # ['hello', 'hello', 'hello', 'hello']
# 3. 元组
t1 = ('world',)
print(t1 * 4) # ('world', 'world', 'world', 'world')
in 或 not in
# 1. 字符串
print('a' in 'abcd') # True
print('a' not in 'abcd') # False
# 2. 列表
list1 = ['a', 'b', 'c', 'd']
print('a' in list1) # True
print('a' not in list1) # False
# 3. 元组
t1 = ('a', 'b', 'c', 'd')
print('aa' in t1) # False
print('aa' not in t1) # True
公共⽅法
函数 | 描述 |
---|---|
len() | 计算容器中元素个数 |
del 或 del() | 删除 |
max() | 返回容器中元素最⼤值 |
min() | 返回容器中元素最⼩值 |
range(start,end, step) | ⽣成从 start 到 end 的数字,步⻓为 step,供 for 循环使⽤ |
enumerate() | 函数⽤于将⼀个可遍历的数据对象(如列表、元组或字符串)组合为⼀个索引序列,同时列出数据和数据下标,⼀般⽤在 for 循环当中。 |
sum() | 序列求和 |
zip() | 合并系列 |
max()
# 1. 字符串
str1 = 'abcdefg'
print(max(str1)) # g
# 2. 列表
list1 = [10, 20, 30, 40]
print(max(list1)) # 40
min()
# 1. 字符串
str1 = 'abcdefg'
print(min(str1)) # a
# 2. 列表
list1 = [10, 20, 30, 40]
print(min(list1)) # 10
enumerate()
语法
enumerate(可遍历对象, start=0)
list1 = ['a', 'b', 'c', 'd', 'e']
for i in enumerate(list1):
print(i)
for index, char in enumerate(list1, start=1):
print(f'下标是{index}, 对应的字符是{char}')
start 参数⽤来设置遍历数据的下标的起始值,默认为 0
容器类型转换
tuple()
将某个序列转换成元组
list1 = [10, 20, 30, 40, 50, 20]
s1 = {100, 200, 300, 400, 500}
print(tuple(list1))
print(tuple(s1))
list()
将某个序列转换成列表
t1 = ('a', 'b', 'c', 'd', 'e')
s1 = {100, 200, 300, 400, 500}
print(list(t1))
print(list(s1))
set()
将某个序列转换成集合
list1 = [10, 20, 30, 40, 50, 20]
t1 = ('a', 'b', 'c', 'd', 'e')
print(set(list1))
print(set(t1))
二:推导式
-
列表推导式
-
字典推导式
-
集合推导式
-
元组推导式
[表达式 for 变量 in 列表 if 条件]
{健:值 for 变量 in 字典.items() if 条件}
{表达式 for 变量 in 集合 if 条件}
(表达式 for 变量 in 元组 if 条件)
⽤⼀个表达式创建⼀个有规律的列表或控制⼀个有规律列表。
列表推导式
创建一个空列表,追加 1 到 10 这几个数字
- for 循环实现过程
list1 = []
for i in range(1,11):
list1.append(i)
print(list1)
- 列表推导式实现过程
list1 = [i for i in range(1,11)]
print(list1)
带 if 的列表推导式
将 1 到 10 的偶数添加到列表
方法一:利用步长实现
list1 = [i for i in range(0, 11, 2)]
print(list1)
方法二:if 实现
list1 = [i for i in range(10) if i % 2 == 0]
print(list1)
字典推导式
创建⼀个字典:字典 key 是 1-5 数字,value 是这个数字的 2 次⽅
# dict1 = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
dict1 = {i: i**2 for i in range(1, 6)}
print(dict1)
将两个列表变为⼀个字典
list1 = ['name', 'age', 'sex']
list2 = ['Tom', 20, '男']
dict1 = {list1[i]: list2[i] for i in range(len(list1))}
print(dict1)
提取字典中⽬标数据
computs = {'AUC': 268, 'HP': 125, 'DELL': 201, 'Lenovo': 199, 'acer': 99}
# 需求:提取上述电脑数量⼤于等于200的字典数据
count1 = {key: value for key, value in computs.items() if value >= 200}
print(count1) # {'MBP': 268, 'DELL': 201}
集合推导式
计算数字 1,2,3 的平方数
setnew = {i**2 for i in (1,2,3)}
print(setnew)
输出非 abc 的字母
a = {x for x in 'abracadabra' if x not in 'abc'}
print(a)
{'d', 'r'}
元组推导式
生成包含 1 到 9 数字的元组
a = (x for x in range(1,10))
print(a)
# 返回的是生成器对象
# <generator object <genexpr> at 0x7faf6ee20a50>
# 使用 tuple() 函数,可以直接将生成器对象转换成元组
print(tuple(a))
(1, 2, 3, 4, 5, 6, 7, 8, 9)
print(tuple((x for x in range(1,10))))
(1, 2, 3, 4, 5, 6, 7, 8, 9)
久违的给大家留个家庭作业吧~ 一定要仔细阅读哦!
作业
用列表推导式找出下面列表当中不包含“e”的放到新列表中
list1 = [“hello”,“python”,“love”,“you”]
输出结果为
>>>[‘python’, ‘you’]
dict_data = {
"current":1,
"limit":20,
"count":408092,
"list":[
{
"id":1380960,
"prodName":"西芹",
"prodCat":"蔬菜",
"place":"辽冀",
"pubDate":"2022-12-28 00:00:00",
},
{
"id":1380959,
"prodName":"菠菜",
"prodCat":"蔬菜",
"place":"冀鲁",
"pubDate":"2022-12-28 00:00:00",
},
{
"id":1380958,
"prodName":"莴笋",
"prodCat":"蔬菜",
"place":"鲁",
"pubDate":"2022-12-28 00:00:00",
},
{
"id":1380957,
"prodName":"团生菜",
"prodCat":"蔬菜",
"place":"冀京云",
"pubDate":"2022-12-28 00:00:00",
},
{
"id":1380956,
"prodName":"散叶生菜",
"prodCat":"蔬菜",
"place":"冀辽",
"pubDate":"2022-12-28 00:00:00",
},
{
"id":1380955,
"prodName":"罗马生菜",
"prodCat":"蔬菜",
"place":"冀云",
"pubDate":"2022-12-28 00:00:00",
},
{
"id":1380954,
"prodName":"油菜",
"prodCat":"蔬菜",
"place":"鲁京川皖",
"pubDate":"2022-12-28 00:00:00",
},
{
"id":1380953,
"prodName":"香菜",
"prodCat":"蔬菜",
"place":"冀",
"pubDate":"2022-12-28 00:00:00",
},
{
"id":1380952,
"prodName":"茴香",
"prodCat":"蔬菜",
"place":"冀",
"pubDate":"2022-12-28 00:00:00",
},
{
"id":1380951,
"prodName":"韭菜",
"prodCat":"蔬菜",
"place":"冀粤",
"pubDate":"2022-12-28 00:00:00",
},
{
"id":1380950,
"prodName":"苦菊",
"prodCat":"蔬菜",
"place":"冀辽",
"pubDate":"2022-12-28 00:00:00",
},
{
"id":1380949,
"prodName":"油麦菜",
"prodCat":"蔬菜",
"place":"冀辽云",
"pubDate":"2022-12-28 00:00:00",
},
{
"id":1380948,
"prodName":"散菜花",
"prodCat":"蔬菜",
"place":"豫鲁陕云",
"pubDate":"2022-12-28 00:00:00",
},
{
"id":1380947,
"prodName":"绿菜花",
"prodCat":"蔬菜",
"place":"苏鄂浙",
"pubDate":"2022-12-28 00:00:00",
}
]
}
"id":1380959,
"prodName":"菠菜",
"prodCat":"蔬菜",
"place":"冀鲁",
"pubDate":"2022-12-28 00:00:00"
# 提取值 1380959 "菠菜" "蔬菜" "冀鲁" "2022-12-28 00:00:00"
在这里给大家分享一些免费的课程供大家学习,下面是课程里面的截图,点击蓝色字体也可以进行跳转哦~点击这里
1.Python所有方向的学习路线
2.学习软件
工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。
三、学习资料
四、实战资料
实践是检验真理的唯一标准。这里的压缩包可以让你再闲暇之余帮你提升你的个人能力。
五、视频课程
好啦今天的分享就到这里结束了,快乐的时光总是短暂呢,想学习更多课程的小伙伴不要着急,有更多惊喜哦~