一、 list列表扩展的方式有几种(或者说添加元素的方法)
1.append(self, object, /)
将对象追加到列表末尾
2.extend(self, iterable, /)
通过从可迭代对象追加元素来扩展列表。(添加的是序列)
3.insert(self, index, object, /)
在索引之前插入对象
二、对["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]进行默认排序
1.正序
fruit_list = ["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]
fruit_list.sort()
print(fruit_list)
2.倒序
fruit_list = ["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]
fruit_list.sort(reverse=True)
print(fruit_list)
3.根据最后一个字母排序
def sort_func(x):
return x[-1]
fruit_list = ["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]
fruit_list.sort(key=sort_func)
print(fruit_list)
4.先按第一个字母排序,再按最后一个字母排序
def sort_func(x):
return (x[0], x[-1])
fruit_list = ["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]
fruit_list.sort(key=sort_func)
print(fruit_list)
三、dict中所有方法的使用
1.clear(self, /)
Remove all items from list.
dict_data = {1: 1, 2: 2}
data = dict_data.clear()
print(dict_data)
2.copy(self, /)
Return a shallow copy of the list.
dict_data = {1: 1, 2: 2}
data = dict_data.copy()
print(dict_data, data)
3.items(...)
D.items() -> a set-like object providing a view on D's items
一个类似集合的对象,提供 D 项的视图
dict_data = {1: 1, 2: 2, 3: 3}
data = dict_data.items()
print(dict_data, data)
4.get(self, key, default=None, /)
Return the value for key if key is in the dictionary, else default.
如果 key 在字典中,则返回 key 的值,否则返回默认值。
dict_data = {1: 1, 2: 2}
data = dict_data.get(1)
print(dict_data, data)
5. keys(...)
D.keys() -> a set-like object providing a view on D's keys
D.keys() ->一个类似集合的对象,提供 D 键的视图
dict_data = {1: 1, 2: 2}
data = dict_data.keys()
print(dict_data, data)
6.pop(...)
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
D.pop(k[,d]) -> v,删除指定的键并返回相应的
dict_data = {1: 1, 2: 2, 3: 4}
data = dict_data.pop(3)
print(dict_data, data)
If key is not found, default is returned if given, otherwise KeyError is raised
如果未找到 key,则返回 default(如果给定),否则将触发KeyError
dict_data = {1: 1, 2: 2, 3: 4}
data = dict_data.pop(4)
print(dict_data, data)
7.popitem(self, /)
Remove and return a (key, value) pair as a 2-tuple.((key, value))
删除(键、值)对并将其作为 2 元组返回。((键、值))
dict_data = {1: 1, 2: 2, 3: 3}
data = dict_data.popitem()
print(dict_data, data)
![](https://i-blog.csdnimg.cn/blog_migrate/05efb6f25e9fe523fd21019a020d55da.png)
Pairs are returned in LIFO (last-in, first-out) order.
成对按后进先出(后进先出)顺序返回。
Raises KeyError if the dict is empty.
如果字典为空则引发keyerror
8.setdefault(self, key, default=None, /)
Insert key with a value of default if key is not in the dictionary.
如果键不在字典中,则插入值为默认值的键。
dict_data = {1: 1, 2: 2, 3: 3}
data = dict_data.setdefault(4)
print(dict_data, data)
![](https://i-blog.csdnimg.cn/blog_migrate/62746b97216867e8ade354b6b17f3bf4.png)
Return the value for key if key is in the dictionary, else default.
如果 key 在字典中,则返回 key 的值,否则返回默认值。
dict_data = {1: 1, 2: 2, 3: 3}
data = dict_data.setdefault(2)
print(dict_data, data)
9.update(...)
D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
D.update([E, ]**F) -> None.从字典/可迭代 E 和 F 更新 D
dict_data = {1: 1, 2: 2, 3: 3}
dict_data1 = {4: 5, 6: 7, 8: 9}
dict_data.update(dict_data1)
print(dict_data)
![](https://i-blog.csdnimg.cn/blog_migrate/6e71b9feb95390a716c715077861d738.png)
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() 方法,则为:对于 k,V 在 E: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(...)
D.values() -> an object providing a view on D's values
D.values() ->一个对象,提供 D 值的视图
dict_data = {1: 1, 2: 2, 3: 3}
data = dict_data.values()
print(dict_data, data)