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

python第二次作业

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

python第二次作业

1.list和tuple的区别

首先tuple元组内的元素无法被赋值,list列表内元素可以被赋值

然后tuple可使用的方法仅有count计数和index索引两种,而list可使用的方法有10种

2.学到的序列仅有string字符串,bytes字节,tuple元组,list列表

3.list中的方法使用

共11种方法

 |  append(self, object, /)
 |      Append object to the end of the list.(将对象附加到列表的末尾)

 |  clear(self, /)
 |      Remove all items from list.(从列表中删除所有项目)

 |  copy(self, /)
 |      Return a shallow copy of the list.(返回列表的浅拷贝副本)

 |  count(self, value, /)
 |      Return number of occurrences of value.(返回值的出现次数)

 |  extend(self, iterable, /)
 |      Extend list by appending elements from the iterable.(添加可迭代的元素来扩展列表)

 |  index(self, value, start=0, stop=9223372036854775807, /)
 |      Return first index of value.(返回第一个索引到的值)
 |      
 |      Raises ValueError if the value is not present.(如果值不存在,则引发ValueError)

 |  insert(self, index, object, /)
 |      Insert object before index.(在索引之前插入对象)

 |  pop(self, index=-1, /)
 |      Remove and return item at index (default last).(删除并返回索引处的项目(默认为最后一个))
 |      
 |      Raises IndexError if list is empty or index is out of range.(如果列表为空或索引超出范围,则引发IndexError)

 |  remove(self, value, /)
 |      Remove first occurrence of value.(删除第一次出现的值)
 |      
 |      Raises ValueError if the value is not present.

 |  reverse(self, /)
 |      Reverse *IN PLACE*.(位反转)

 |  sort(self, /, *, key=None, reverse=False)
 |      Sort the list in ascending order and return None.(按升序对列表排序,然后返回空值)

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

 

 5.dict中的方法使用

共10种

 |  clear(...)
 |      D.clear() -> None.  Remove all items from D.

 |  copy(...)
 |      D.copy() -> a shallow copy of D

 |  get(self, key, default=None, /)
 |      Return the value for key if key is in the dictionary, else default.(如果键在字典中,则返回键的值,否则为默认值)

 |  items(...)
 |      D.items() -> a set-like object providing a view on D's items(一个类似集合的对象,提供D项的视图)

 |  keys(...)
 |      D.keys() -> a set-like object providing a view on D's keys(提供D键视图的类似集合的对象)

 |  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(如果找不到键,则返回默认值(如果给定),否则会引发KeyError)

 |  popitem(self, /)
 |      Remove and return a (key, value) pair as a 2-tuple.(移除(键、值)对并将其作为2元组返回)
 |      
 |      Pairs are returned in LIFO (last-in, first-out) order.(对按后进先出的顺序返回)
 |      Raises KeyError if the dict is empty.(如果dict为空,则引发KeyError)

 |  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.(如果键在字典中,则返回键的值,否则为默认值)

 |  update(...)(更新,如果被更新的字典中己包含对应的键值对,那么原 value 会被覆盖;如果被更新的字典中不包含对应的键值对,则该键值对被添加进去)
 |      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]

 |  values(...)
 |      D.values() -> an object providing a view on D's values(提供D值视图的对象)

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

b = a.copy(): 浅拷贝, a 和 b 是一个独立的对象,但他们的子对象还是指向统一对象(是引用)

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

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

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