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

python语言基础(二)

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

python语言基础(二)

目录

一、list和tuple的区别

二、学到的序列有哪几种

三、list中所有方法的使用

四、将元组和列表转换成字典

五、dict中所有方法的使用

六、list或dict浅拷贝画图加代码解释


一、list和tuple的区别

(1)list 是可变的对象,元组 tuple 是不可变的对象。

(2)tuple 是不可变的,它可以作为 dict 的 key,而 list 则不行。

二、学到的序列有哪几种

四种:str(字符串)、bytes(字节),tuple(元组)和list(列表)

三、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.

 

(9)移除(报错)remove(self, value, /)

      Remove first occurrence of value.

     

      Raises ValueError if the value is not present.

 

(10)倒置(反转)reverse(self, /)

      Reverse *IN PLACE*.

 

(11)排序sort(self, /, *, key=None, reverse=False)

      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.

四、将元组和列表转换成字典

(1)将元组转换成字典

(2)将列表转换成字典

五、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.

 

(4)items(...)

      D.items() -> a set-like object providing a view on D's items

 

(5)keys(...)

      D.keys() -> a set-like object providing a view on D's keys

 

(6)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

 

(7)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.

 

(8)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.

  

(9)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]

 

(10)values(...)

      D.values() -> an object providing a view on D's values

六、list或dict浅拷贝画图加代码解释

(1)list

list其本质就是数组,而其存储的形式是地址。如图所示,将List A列表复制时,其实相当于A的内容复制给了B,python中相同内容的数组指向同一地址,即进行浅拷贝后A与B指向同一地址。后果就是,改变B的同时也会改变A,因为改变B就是改变B所指向地址的内容,由于A也指向同一地址,所以A与B一起改变,这也就是List的浅拷贝。

 

(2)dict

dict浅拷贝的a和b两者是独立的,但里面的列表还是同一个,因此a列表改变后b列表也会随之改变。

 

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

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

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