首页 > 编程语言 >跟艾文学编程《零基础入门学Python》(2)Python 容器

跟艾文学编程《零基础入门学Python》(2)Python 容器

时间:2023-03-17 21:07:22浏览次数:42  
标签:... set 入门 tuple Python 编程 list 列表 dict



作者: 艾文,计算机硕士学位,企业内训讲师和金牌面试官,公司资深算法专家,现就职BAT一线大厂。
 
内容:跟艾文学编程《零基础入门学Python​​​​​​​》

学习目标

  • 列表List
  • 列表推导式
  • tuple元组
  • set 集合
  • dict 字典
  • 实战项目

列表List

list 是一种有序的集合,可以随时的添加、删除一个元素

创建list

创建一个list,只有逗号分割不同的数据,然后使用方括号扩起来。list 中的元素可以是不同的数据类型

跟艾文学编程《零基础入门学Python》(2)Python 容器_numpy

跟艾文学编程《零基础入门学Python》(2)Python 容器_numpy_02

访问list 数值

跟艾文学编程《零基础入门学Python》(2)Python 容器_数据_03

list 更新

跟艾文学编程《零基础入门学Python》(2)Python 容器_seaborn_04

我们希望被添加到list 中的数据,每个元素做为添加对象.

跟艾文学编程《零基础入门学Python》(2)Python 容器_pandas_05

注意:

list 集合 extend 和 append 的区别

list 删除

跟艾文学编程《零基础入门学Python》(2)Python 容器_seaborn_06

list 运算符

跟艾文学编程《零基础入门学Python》(2)Python 容器_seaborn_07

再次强调: extend 、append 的区别

list 中函数

跟艾文学编程《零基础入门学Python》(2)Python 容器_pandas_08

跟艾文学编程《零基础入门学Python》(2)Python 容器_numpy_09

上边的内容不满足我们的业务需求,我们更佳希望是按照数量进行排序

跟艾文学编程《零基础入门学Python》(2)Python 容器_数据_10

list 的列表推导式

跟艾文学编程《零基础入门学Python》(2)Python 容器_数据_11

这块代码有哪些可以进一步优化的呢?

1.一个简单的顾虑操作,5行代码才完成此功能

2.更佳简单、效果可视化感觉最好的方式处理?

跟艾文学编程《零基础入门学Python》(2)Python 容器_pandas_12

tuple 元组

tuple 和 list 非常类似,但是有区别

  • tuple 初始化之后不能修改了
  • tuple 没有append insert 函数操作原来的tuple
  • tuple 同样提供的索引获取数据函数
  • 建议list 和tuple ,一般情况能用tuple 的就不用list ,使得我们的操作更佳安全

跟艾文学编程《零基础入门学Python》(2)Python 容器_pandas_13

读取tuple 类型的数据

跟艾文学编程《零基础入门学Python》(2)Python 容器_numpy_14

更新tuple

tuple 不可变,不能修改,和list 的区别

跟艾文学编程《零基础入门学Python》(2)Python 容器_seaborn_15

删除 tuple

不能删除,tuple 不能变

跟艾文学编程《零基础入门学Python》(2)Python 容器_numpy_16

tuple 运算符

跟艾文学编程《零基础入门学Python》(2)Python 容器_数据_17

tuple 内置函数

跟艾文学编程《零基础入门学Python》(2)Python 容器_numpy_18

set

是一个无序不重复的元素的集合。基本功能: 消除重复数据、关系测试。 set和dict 类似,但是set 不存在value

set 创建

跟艾文学编程《零基础入门学Python》(2)Python 容器_seaborn_19

add(...)

| Add an element to a set. |

| This has no effect if the element is already present.

跟艾文学编程《零基础入门学Python》(2)Python 容器_numpy_20

remove(self, value, /)

| Remove first occurrence of value. |

| Raises ValueError if the value is not present.

跟艾文学编程《零基础入门学Python》(2)Python 容器_pandas_21

union(...)

| Return the union of sets as a new set. |

| (i.e. all elements that are in either set.)

跟艾文学编程《零基础入门学Python》(2)Python 容器_python_22

intersection(...)

| Return the intersection of two sets as a new set. |

| (i.e. all elements that are in both sets.)

跟艾文学编程《零基础入门学Python》(2)Python 容器_数据_23

difference(...)

| Return the difference of two or more sets as a new set. |

| (i.e. all elements that are in this set but not the others.)

跟艾文学编程《零基础入门学Python》(2)Python 容器_数据_24

dict 字段

字典是一种可变容器模型,可以存储任意类型对象。

语法: _dict = {key1:value1,key2:value2}

dict 创建和访问

跟艾文学编程《零基础入门学Python》(2)Python 容器_numpy_25

dict 新增或者修改

跟艾文学编程《零基础入门学Python》(2)Python 容器_python_26

dict 遍历

跟艾文学编程《零基础入门学Python》(2)Python 容器_python_27

实战项目

序列中的数据去重

跟艾文学编程《零基础入门学Python》(2)Python 容器_python_28

序列中单词长度长于3 返回

跟艾文学编程《零基础入门学Python》(2)Python 容器_数据_29

可以使用列表推导式来解决该问题。

跟艾文学编程《零基础入门学Python》(2)Python 容器_seaborn_30

两个list 序列相同的元素输出

跟艾文学编程《零基础入门学Python》(2)Python 容器_numpy_31

词频统计

跟艾文学编程《零基础入门学Python》(2)Python 容器_pandas_32

还有没有更加简单的方法呢?

跟艾文学编程《零基础入门学Python》(2)Python 容器_numpy_33

两个list 列表,建立一一对应的关系

跟艾文学编程《零基础入门学Python》(2)Python 容器_pandas_34

最后,让我们一起加油!!!

 

标签:...,set,入门,tuple,Python,编程,list,列表,dict
From: https://blog.51cto.com/u_14361901/6128371

相关文章