目录
1. list列表扩展的方式有几种(或者说添加元素的方法)
append(self,object,/)在末尾添加对象
insert(self,index,object,/)将对象插入列表
extend(self,iterable,/)扩展列表
2. 对["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]进行默认排序
对上面的列表使用第三个字母进行排序
![](https://img-blog.csdnimg.cn/9b4ab460a83244349b9fd0347e396321.png)
3. dict中所有方法的使用(先写源代码再写样例)
(1)clear(...)删除所有元素
D.clear ()——> None
.从D中删除所有项目。
(2)copy(...) 拷贝
D.copy()——>a shallow copy of D
D.copy()——>D的浅层副本
3)fromkeys(*args, **kwargs)
Create a new dictionary with keys from iterable and values set to value
创建一个新字典,其中包含可迭代的键并将值设置为值
(4)items(...)
D.items() -> a set-like object providing a view on D's items
一个类似集合的对象,提供 D 项的视图
(5)-keys(...)
D.keys() -> a set-like object providing a view on D's keys
一个类似集合的对象,提供 D 键的视图
6)get(self, key, default=None, /)|
|Return the value for key if key is in the dictionary, else default. |
如果 key 在字典中,则返回 key 的值,否则返回默认值
(7)pop(...)
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
D.pop(k[,d]) -> v,删除指定的键并返回相应的值。
8)popitem(self, /) Remove and return a (key, value) pair as a 2-tuple.((key, value))
删除(键、值)对并将其作为 2 元组返回。((键、值))。
Pairs are returned in LIFO (last-in, first-out) order. 成对按后进先出(后进先出)顺序返回。
Raises KeyError if the dict is empty. 如果字典为空,则引发密钥cuowu
(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. 如果 key 在字典中,则返回 key 的值,否则返回默认值
(10)values(...)
D.values() -> an object providing a view on D's values
D.values() ->一个对象,提供 D 值的视图