列表 List
List里面也可以加其他元素
AI 人工智能基础课 python-2(1)List 列表
list=[ ] 声明一个空列表

看了第一个list((1,2,3))突然有些混淆,于是试验了下面几种情况,不过还是有点迷糊,可能等以后就能慢慢有感觉了。
AI 人工智能基础课 python-2(1)List 列表
AI 人工智能基础课 python-2(1)List 列表
我想看看里面的第三个人在不在:
AI 人工智能基础课 python-2(1)List 列表
1包含,5不包含
AI 人工智能基础课 python-2(1)List 列表
还以为-2会是从倒数第二个往前推呢。。。。
AI 人工智能基础课 python-2(1)List 列表
增加 append:
AI 人工智能基础课 python-2(1)List 列表
更改:
AI 人工智能基础课 python-2(1)List 列表
增加一个列表:
AI 人工智能基础课 python-2(1)List 列表
AI 人工智能基础课 python-2(1)List 列表
为什么第一种情况list+[ ],list加上一个列表是错的呢,
因为无论是list 还是新加上的列表,都是独立的,只是纯粹拼在一起,
不是在list那个列表里面增加了一个元素。

所以会出现下面这种情况:
小G根本不在list里面,所以不能移除
AI 人工智能基础课 python-2(1)List 列表
小E在list里面,所以可以移除
AI 人工智能基础课 python-2(1)List 列表
AI 人工智能基础课 python-2(1)List 列表
删除 remove:
AI 人工智能基础课 python-2(1)List 列表
pop 和 remove的区别:
1.remove你得知道片段里面的数值是什么,比如我得知道里面是小A,小B,小C,小D,小E,才能移除
但是pop只要知道,我要移除第几个数值就行了,比如:
AI 人工智能基础课 python-2(1)List 列表
2.remove就是移除,但是pop是移除以后,可以返回这个值,就是把这个移除的值保留下来
AI 人工智能基础课 python-2(1)List 列表
3.pop()是默认删除最后一个
AI 人工智能基础课 python-2(1)List 列表
AI 人工智能基础课 python-2(1)List 列表
列表反转
AI 人工智能基础课 python-2(1)List 列表
AI 人工智能基础课 python-2(1)List 列表
AI 人工智能基础课 python-2(1)List 列表
其实我找了一下,有人说是这个意思:
[::-1]代表从后向前取值,每次步进值为1

(先暂时理解为:所有以后,请所有往前吧)
AI 人工智能基础课 python-2(1)List 列表
在这里,list是没有改变的,因为我们把反转的list赋值给了list_2

删除 del
AI 人工智能基础课 python-2(1)List 列表
AI 人工智能基础课 python-2(1)List 列表
感觉每次都会犯这种错误

增加 extend
AI 人工智能基础课 python-2(1)List 列表
extend和append的区别:
AI 人工智能基础课 python-2(1)List 列表

相关文章: