1 list列表扩展的方式有几种(或者说添加元素的方法)
(1).append(self, object, /) 在末尾添加对象
(2).insert(self, index, object, /) 将对象插入列表
(3).extend(self, iterable, /) 扩展列表2. 对["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]进行默认排序
对上面的列表使用第三个字母进行排序
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.
如果字典为空,则引发密钥错误
(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)update(self,E=None,**F):
D.update([E,]**F)->None.Update D from dict/iterable E and F.
从字典/可迭代 E 和 F 更新 D。
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
如果 E 存在并且具有 .keys() 方法,则对于 E 中的 k:D[k] = E[k]
If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
如果 E 存在并且缺少 .keys() 方法,则对于 E 中的 k, v:D[k] = v
In either case, this is followed by: for k in F: D[k] = F[k]
在任何一种情况下,这后面跟着:对于F中的k:D[k] = F[k]
(11)values(...)
D.values() -> an object providing a view on D's values
D.values() ->一个对象,提供 D 值的视图