当前位置:网站首页>学习总结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]])
边栏推荐
- Excel Paint
- Transformation 和 Action 常用算子
- redis 缓存清除策略
- 2022牛客多校第四场C.Easy Counting Problem(EGF+NTT)
- Flutter真机运行及模拟器运行
- Flutter TapGestureRecognizer 如何工作
- Mysql5.7 二进制 部署
- Detailed Explanation of Redis Sentinel Mode Configuration File
- The role of the range function
- Using pip to install third-party libraries in Pycharm fails to install: "Non-zero exit code (2)" solution
猜你喜欢

RL强化学习总结(一)
![[cesium] element highlighting](/img/99/504ca9802db83eb33bc6d91b34fa84.png)
[cesium] element highlighting

Flutter learning 2-dart learning

Wise Force Deleter强制删除工具

第四讲 back propagation 反向传播

Structured light 3D reconstruction (1) Striped structured light 3D reconstruction

Geek卸载工具

【过一下7】全连接神经网络视频第一节的笔记
![[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)](/img/86/9c9a2541f2b7089ae47e9832fffdb3.png)
[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)

Qt制作18帧丘比特表白意中人、是你的丘比特嘛!!!
随机推荐
Requests the library deployment and common function
Xiaobai, you big bulls are lightly abused
判断语句_switch与case
server disk array
vscode+pytorch use experience record (personal record + irregular update)
Flutter learning three-Flutter basic structure and principle
What field type of MySQL database table has the largest storage length?
逆向理论知识4
Convert the paper official seal in the form of a photo into an electronic official seal (no need to download ps)
【过一下4】09-10_经典网络解析
【过一下3】卷积&图像噪音&边缘&纹理
开发一套高容错分布式系统
【过一下16】回顾一下七月
【过一下14】自习室的一天
【过一下6】机器视觉视频 【过一下2被挤掉了】
The role of the range function
Flutter Learning 4 - Basic UI Components
How can Flutter parent and child components receive click events
Excel Paint
Returned object not currently part of this pool