栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python中list及dict详解

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python中list及dict详解

这里写自定义目录标题
  • 1.list和tuple的区别
  • 2.学到的序列有哪几种
  • 3.list中所有方法的使用
  • 4.将元组和列表转换成字典
  • 5.dict中所有方法的使用
  • 6.list或dict浅拷贝画图加代码解释

1.list和tuple的区别

1,list中的元素可修改,tuple中的元素不可修改。但是如果tuple中嵌套了list,那么由于list本身可修改,故此时的tuple也算是可以修改的。

2,list的元素写在 [ ] 中,tuple中的元素写在 () 中。

3,构建只有一个元素的list,可以直接输入一个元素。但是在tuple中,如果要设置一个元素的元组,需要在元素后方加个英文逗号:’ , '。否则需要打印整个元组的时候,就只能打印元组中的元素。

2.学到的序列有哪几种

1,字符串
2,list列表
3,元组

三者都可以通过下表访问其中的元素,且默认索引值都是从0开始

3.list中所有方法的使用

1,append(self, object, /) 在列表的末尾追加一个元素,无返回值
Append object to the end of the list.

2,clear(self, /) 清除列表中的所有元素,无返回值
Remove all items from list.

3,copy(self, /) 浅拷贝
Return a shallow copy of the list.

4,count(self, value, /) 统计列表中某元素出现的次数,有返回值
Return number of occurrences of value.

5,extend(self, iterable, /) 增加可迭代对象,在列表尾部 分别添加 每个 可迭代对象
Extend list by appending elements from the iterable.

6,index(self, value, start=0, stop=9223372036854775807, /) 索引目标元素在该列表第一次出现的位置
Return first index of value.

| Raises ValueError if the value is not present.

7,insert(self, index, object, /) 在列表指定的位置插入元素
Insert object before index.

8,pop(self, index=-1, /) 删除指定位置的元素
Remove and return item at index (default last). 返回值为被删除的元素

| Raises IndexError if list is empty or index is out of range.

8,remove(self, value, /) 删除该列表中目标元素首次出现的值及其位置
Remove first occurrence of value.
若删除的元素不存在,会报错;可以先用count方法检查元素是否存在
Raises ValueError if the value is not present.

9,reverse(self, /) 倒置,并不是排序
Reverse IN PLACE.

10,sort(self, /, *, key=None, reverse=False) 排序,默认是升序
'/'代表左边不能用关键字传参 ’ *'代表右侧必须使用关键字传参
key可以自行构建规则
例如,我们打算用最后一个字母以ascii码表来排序,若相同,则用倒数第二个字母排序,若还相同,则用倒数第三个字母排序。

| Sort the list in ascending order and return None.
|
| The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
| order of two equal elements is maintained).
|
| If a key function is given, apply it once to each list item and sort them,
| ascending or descending, according to their function values.
|
| The reverse flag can be set to sort in descending order.
|

4.将元组和列表转换成字典

1,将元组转化成字典

2,将列表转换成字典

5.dict中所有方法的使用

| 1,clear(…) 清除字典中的所有元素,无返回值
| D.clear() -> None. Remove all items from D.

| 2,copy(…) 浅拷贝
| D.copy() -> a shallow copy of D
|
| 3,get(self, key, default=None, /) 获取字典中对应下标的元素,且有返回值
| Return the value for key if key is in the dictionary, else default.
| 5,items(…)
| D.items() -> a set-like object providing a view on D’s items |
| 6,keys(…)
| D.keys() -> a set-like object providing a view on D’s keys
|
| 7,pop(…)
| D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
|
| If key is not found, default is returned if given, otherwise KeyError is raised
|
| 8,popitem(self, /)
| Remove and return a (key, value) pair as a 2-tuple.
|
| Pairs are returned in LIFO (last-in, first-out) order.
| Raises KeyError if the dict is empty.
|
| 9,setdefault(self, key, default=None, /)
| Insert key with a value of default if key is not in the dictionary.
|
| Return the value for key if key is in the dictionary, else default.
|
| 10,update(…)
| D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
| If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
| If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
| In either case, this is followed by: for k in F: D[k] = F[k]
|
| 11,values(…)
| D.values() -> an object providing a view on D’s values
|

6.list或dict浅拷贝画图加代码解释

以list为例:
使用python tutor网页演示浅拷贝,

在代码后添加:
c[2][2][1] = 7

根据可视化代码执行过程可以看到 a,b,c 三组列表对应元素都发生了改变

再加代码修改为:
c[1]=7:

可以看到只有c列表发生了改变。
从而我们可以得出结论,python中浅拷贝是发生在子列表内,对第一层列表没有影响

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/960414.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号