目录
对["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]进行默认排序
一、 list列表扩展的方式有几种(或者说添加元素的方法)
有三种
1、append 赋值
2、insert 插入
3、extend 扩展
二、排序
对["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]进行默认排序
对上面的列表使用第三个字母进行排序
三、 dict中所有方法的使用(先写源代码再写样例)
1、clear
- clear(self):
- """D.clear()->None.RemoveallitemsfromD."""
- 删除所有元素
2、copy
- copy(self):
- """D.copy()->ashallowcopyofD"""
- 拷贝
3、get
- get(self,*args,**kwargs):
- """Return the value for key if key is in the dictionary,else default."
- 如果 key 在字典中,则返回 key 的值,否则返回默认值。
4、items
- items(self):
- """D.items()->aset-like object providing aview on D'sitems"""
- 一个类似集合的对象,提供字典项目的视图
5、keys
- keys(self):
- """D.keys()->aset-like object providing aview on D'skeys"""
- 一个类似集合的对象,提供 字典键上的视图
6、pop
- pop(self,k,d=None):
- D.pop(k[,d])->v,removespecifiedkeyandreturnthecorrespondingvalue.
- 删除指定的键并返回相应的值
- If key is not found, default is returned if given, otherwise KeyError is raised
- 如果未找到 key,则返回 default(如果给定),否则引发 KeyError
7、popitem
- popitem(self,*args,**kwargs):
- Removeandreturna(key,value)pairasa2-tuple.
- 删除(键、值)对并将其作为 二元组返回。
- airs are returned in LIFO (last-in, first-out) order.
- 成对按后进先出(后进先出)顺序返回。
- Raises KeyError if the dict is empty.
- 如果字典为空,则引发 KeyError。
8、setdefault
- setdefault(self,*args,**kwargs):
- Insertkeywithavalueofdefaultifkeyisnotinthedictionary.
- 如果键不在字典中,则插入值为默认值的键。
-
Return the value for key if key is in the dictionary, else default
-
如果 key 在字典中,则返回 key 的值,否则返回默认值。
9、update
- 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]
10、values
- values(self):
- D.values()->an object providing aview on D'svalues
- 在任何一种情况下,这后面跟着:对于F中的k:D[k] = F[k]