目录
1.list和tuple的区别
首先list 与 tuple 都是序列类型的容器对象,可以存放任何类型的数据、支持切片、迭代等操作。
它们最本质的区别是,list是可变数据类型,tuple是不可变数据类型。
list用的是[ ],tuple用的是( )。
tuple没有insert,pop,append方法。
如果tuple里的元素是list时可变。
2.学到的序列有哪几种
- 字符串(string)
- 字节(bytes)
- 元组(tuple)
- 列表(list)
- 字典(dictionary)
3.list中所有方法的使用
-
append
append(self, object, /)
| Append object to the end of the list. 将对象追加到列表的末尾。
![](https://i-blog.csdnimg.cn/blog_migrate/914440766a0af0005c4a3dc2d4188ec4.png)
-
clear
clear(self, /)
| Remove all items from list. 从列表中删除所有项目。
- count
count(self, value, /)
| Return number of occurrences of value. 返回值出现的次数
![](https://i-blog.csdnimg.cn/blog_migrate/5a4f8016ff6c1902daa0c7c3991c8777.png)
- extend
extend(self, iterable, /)
| Extend list by appending elements from the iterable.通过追加可迭代元素来扩展列表。
![](https://i-blog.csdnimg.cn/blog_migrate/5a64bee0c76335c45d6a664ddc0f59b2.png)
- index
index(self, value, start=0, stop=9223372036854775807, /)
| Return first index of value. 返回值的第一个索引
![](https://i-blog.csdnimg.cn/blog_migrate/02852e1e8aeb8adfc8e094dc83d5b07e.png)
- insert
insert(self, index, object, /)
| Insert object before index.在索引前插入 object
![](https://i-blog.csdnimg.cn/blog_migrate/69f26d4df85b1d57e9f6c8a1d07966dd.png)
- pop
pop(self, index=-1, /) #index=-1,
就是如果不传参数的话,
index
默认等于
-1
#
如果传了参数
: index=value
| Remove and return item at index (default last).
|
| Raises IndexError if list is empty or index is out of range
.删除并返回索引项(默认为最后一项)。如果列表为空或索引超出范围,则引发 Index Error 。
- remove
remove(self, value, /)
Remove first occurrence of value.
Raises ValueError if the value is not present.
删除值的第一次出现。||如果值不存在则引发 Value Error 。
- reverse
reverse(self, /) #
倒置,反置
| Reverse *IN PLACE*
- sort
sort(self, /, *, key=None, reverse=False)
| Sort the list in ascending order and return None. 按升序对列表排序并返回。
- copy
4.将元组和列表转换成字典
![](https://i-blog.csdnimg.cn/blog_migrate/f4bad56b3a2207232b8a00c32732e3bc.png)
5.dict中所有方法的使用
- clear
- copy
- get
- items
- keys
- pop
- popitem
- setdefault
- update
- values
6.list或dict浅拷贝画图加代码解释
b = a:
赋值引用,
a
和
b
都指向同一个对象。
、b = a.copy(): 浅拷贝, a 和 b 是一个独立的对象,但他们的子对象还是指向统一对象(是引用)。
b = copy.deepcopy(a):
深度拷贝
, a
和
b
完全拷贝了父对象及其子对象,两者是完全独立的。
![](https://i-blog.csdnimg.cn/blog_migrate/1be2f8cda1b6ed8937c952c925aef639.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b4beb1c152963138bb038f64e49bbfa1.png)