当前位置:网站首页>学习总结week2_2
学习总结week2_2
2022-08-05 05:13:00 【非鱼丶丶】
python容器型数据
1.列表的比较运算: ==, !=, >, <, >=, <=
==, !=
print([1, 2, 3] == [1, 2, 3])
, <, >=, <=
只能两个列表比较
只和第一段不相等的元素的大小有关
print([10, 10000, 364122, 111, 1, 1, 1] < [10, 2, 55])
2.列表(序列)相关函数
1)sum(列表) - 求所有元素的和(必须都是数字)
print(sum(range(1, 101)))
2)max 、min
max(列表) - 获取列表中的最大元素(元素支持比较运算)
num = [10, 2, 3, 4, 5, 6]
print(max(num))
print(min(num))
3)sorted(列表) - 将列表中的元素从小到大排序,产生一个新的列表
sorted(列表, reverse=True) - 将列表中的元素从大到小排序,产生一个新的列表
num = [10, 2, 3, 4, 5, 6]
print(sorted(num, reverse=True))
4)len(列表) - 统计列表元素个数
5)list(数据)- 将指定数据转换为列表
任何序列都可以转换为列表,转换的时候直接将序列中的元素作为列表的元素
print(list('abc'))
print(list(range(8)))
3.列表方法的使用套路:列表.xxx()
python的变量本质都保存堆区间的地址,地址对应的数据,所有的变量都是指针变量,变量在栈区间
用不了保存数据的时候,变量真正保存的其实是数据在内存中的地址;
当一个变量直接给另外一个变量赋值的时候,赋的其实是变量中保存的地址;
赋值后两个变量指向的是同一块内存。
补充:深拷贝:from copy import deepcopy
b = deepcopy(a)
1.列表.clear() - 清空列表
2.列表.copy() - (浅)拷贝原列表,产生一个一模一样的新列表,将新列表返回
a = [1, 2, 3]
b = a
c = a.copy()
a.append(100)
print(a, b, c)
3.列表.count(数据) - 统计列表中指定数据出现的次数(数据个数)
4.列表.extend(序列) - 将序列中所有的元素全部添加到列表的最后
num = [10, 20, 30]
num.extend('abd')
num.extend([1, 2, 3])
print(num)
5.列表.index(数据) - 获取指定数据在列表中的第一个下标(不存在会报错)
print(num.index(1))
6.列表.reverse() - 列表倒序
列表.sort() - 将列表中的元素按照升序排序(直接修改原列表元素的顺序,不会产生新的列表)
sorted(序列) - 将序列中的元素按照升序排序(不会修改原列表元素的顺序,会产生新的列表
3.列表推导式:本质是一种创建列表的表达式
结构1:
[表达式 for 变量 in 序列]
原理:让变量去序列中取值,一个一个取,每取一个值就计算一次表达式的结果,并将结果作为列表的元素
应用:对序列中的原理进行统一变形
结构2:
[表达式 for 变量 in 序列 if 条件语句]
应用:进行数据筛选
print([10 for x in range(5)])
print([x + 2 for x in range(5) if x % 2 == 0])
# 练习1:提取nums中所有元素的个数
nums = [18, 29, 892, 78, 91, 56]
print([x % 10 for x in nums])
# 练习2:使用列表推导式让所有员工的薪资提高10%
pays = [18290, 10000, 8921, 7828, 12000, 5600]
print([x * 1.1 for x in pays])
# 案例1:获取nums中所有的偶数
nums = [18, 90, 43, 67, 88, 19, 84]
# [18, 90, 88, 84]
print([x for x in nums if x % 2 == 0])
# 案例2:获取列表中所有数字,并且将数字都乘以10
list1 = [10, 2.25, 'abc', False, True, 'as12', 4, 2.5]
# [20, 4.5, 8, 5]
# print([x * 10 for x in list1 if type(x) == int or type(x) == float])
print([x * 10 for x in list1 if type(x) in [int, float]])
边栏推荐
- 【读书】长期更新
- Flutter TapGestureRecognizer 如何工作
- "PHP8 Beginner's Guide" A brief introduction to PHP
- Using QR codes to solve fixed asset management challenges
- Qt制作18帧丘比特表白意中人、是你的丘比特嘛!!!
- OFDM 十六讲 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems
- 【过一下15】学习 lstm的一周
- 分布式和集群
- Develop a highly fault-tolerant distributed system
- span标签和p标签的区别
猜你喜欢
随机推荐
coppercam primer [6]
【过一下8】全连接神经网络 视频 笔记
Do you use tomatoes to supervise your peers?Add my study room, come on together
重新审视分布式系统:永远不会有完美的一致性方案……
Dashboard Display | DataEase Look at China: Data Presents China's Capital Market
Flutter learning three-Flutter basic structure and principle
将照片形式的纸质公章转化为电子公章(不需要下载ps)
Returned object not currently part of this pool
[cesium] 3D Tileset model is loaded and associated with the model tree
The role of DataContext in WPF
The mall background management system based on Web design and implementation
Convert the paper official seal in the form of a photo into an electronic official seal (no need to download ps)
Returned object not currently part of this pool
Lecture 4 Backpropagation Essays
2023 International Conference on Information and Communication Engineering (JCICE 2023)
Analysis of Mvi Architecture
shell函数
How to quickly upgrade your Taobao account to a higher level
【记一下1】2022年6月29日 哥和弟 双重痛苦
【技能】长期更新








![LeetCode: 1403. Minimum subsequence in non-increasing order [greedy]](/img/99/41629dcd84e95eb3672d0555d6ef2c.png)
