0
点赞
收藏
分享

微信扫一扫

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



学习目标

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

列表List

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

创建list

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

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

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

访问list 数值

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

list 更新

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

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

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

注意:

list 集合 extend 和 append 的区别

list 删除

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

list 运算符

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

再次强调: extend 、append 的区别

list 中函数

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

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

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

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

list 的列表推导式

跟艾文学编程《零基础入门学Python》(2)Python 容器_pandas_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 容器_seaborn_13

读取tuple 类型的数据

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

更新tuple

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

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

删除 tuple

不能删除,tuple 不能变

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

tuple 运算符

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

tuple 内置函数

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

set

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

set 创建

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

add(...)

| Add an element to a set. |

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

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

remove(self, value, /)

| Remove first occurrence of value. |

| Raises ValueError if the value is not present.

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

union(...)

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

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

跟艾文学编程《零基础入门学Python》(2)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 容器_pandas_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 容器_pandas_24

dict 字段

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

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

dict 创建和访问

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

dict 新增或者修改

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

dict 遍历

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

实战项目

序列中的数据去重

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

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

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

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

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

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

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

词频统计

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

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

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

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

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

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

 

举报

相关推荐

0 条评论