1、list列表扩展的方式有几种(或者说添加元素的方法)
extend:通过迭代追加来扩展元素
list_data = [1, 2, 3]
str_data = 1, 2, 3
list.extand(list_data, str_data)
print(list_data)
insert:在索引前插入对象
list_data = [1, 2, 3]
list_data.insert(1, 4)
print(list_data)
append:追加到末尾
list_data = [1, 2, 3]
list.append(4)
print(list_data)
2. 对["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]进行默认排序
fruit_data = ["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]
fruit_data.sort()
print(fruit_data)
对上面的列表使用第三个字母进行排序
def sort_func(x):
return x[2]
fruit_data = ["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]
fruit_data.sort(key=sort_func)
print(fruit_data)
3、dict中所有方法的使用(先写源代码再写样例)
clear(self): # real signature unknown; restored from __doc__
""" D.clear() -> None. Remove all items from D. """
删除所有项目
dict_data = {1:1, 2:2}
dict_clear()
print(dict_data)
copy(self): # real signature unknown; restored from __doc__
""" D.copy() -> a shallow copy of D """
浅拷贝
dict_data = {1:1, 2:2}
data = dict_data.copy()
print(dict_data, data)
get(self, *args, **kwargs): # real signature unknown
""" 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)
items(self): # real signature unknown; restored from __doc__
""" D.items() -> a set-like object providing a view on D's items "
一个类似集合的对象,提供字典项目的视图
dict_data = {1: 1, 2: 2, 3: 3}
data = dict_data.keys()
print(dict_data, data)
If key is not found, default is returned if given, otherwise KeyError is raised
dict_data = {1: 1, 2: 2, 3: 4}
data = dict_data.popitem()
print(dict_data, data)
如果未找到key,则返回default(如果给定),否则引发KeyError
dict_data = {1: 1, 2: 2, 3: 4}
data = dict_data.pop(4)
print(dict_data, data)
popitem(self, *args, **kwargs): # real signature unknown
"""
Remove and return a (key, value) pair as a 2-tuple.
删除(键、值)对并将其作为 二元组返回。
dict_data = {1: 1, 2: 2, 3: 4}
data = dict_data.popitem()
print(dict_data, data)
Pairs are returned in LIFO (last-in, first-out) order.
dict_data = {1: 1, 2: 2, 3: 4}
data = dict_data.setdefault(4)
print(dict_data, data)
成对按后进先出(后进先出)顺序返回
Raises KeyError if the dict is empty.
如果字典为空,则引发 KeyErro
setdefault(self, *args, **kwargs): # real signature unknown
"""
Insert key with a value of default if key is not in the dictionary.
如果键不在字典中,则插入值为默认值的键。
dict_data = {1: 1, 2: 2, 3: 4}
data = dict_data.setdefault(4)
print(dict_data, data)
Return the value for key if key is in the dictionary, else default. """如果 key 在字典中,则返回 key 的值,否则返回默认值。
dict_data = {1: 1, 2: 2, 3: 4}
data = dict_data.setdefault(3)
print(dict_data, data)
update(self, E=None, **F): # known special case of dict.update
"""
D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
从字典/可迭代 E 和 F 更新 D
dict_data = {1: 1, 2: 2, 3: 4}
dict_data1 = {1: 6, 4: 5, 5: 6}
dict_data.update(dict_data1)
print(dict_data)
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]
values(self): # real signature unknown; restored from __doc__
""" D.values() -> an object providing a view on D's values """
在任何一种情况下,这后面跟着:对于F中的k:D[k] = F[k]
dict_data = {1: 1, 2: 2, 3: 4}
data = dict_data.values()
print(dict_data, data)