目录
list_data = [1, 2, 3]
list.append(list_data, 4)
print(list_data)
结果
[1, 2, 3,4]
list_data = [1, 2, 3]
str_data = '123'
list.extend(list_data, str_data)
print(list_data)
结果
[1,2,3,'1','2','3']
list_data = [1, 2, 3]
list_data.insert(1, 4)
print(list_data)
结果
[1, 4, 2, 3]
fruit_data = ["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]
fruit_data.sort()
print(fruit_data)
结果
['cherry', 'durian', 'litchi', 'mangosteen', 'pineapple', 'pitaya', 'pomelo', 'strawberry']
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)
结果
['cherry', 'pomelo', 'mangosteen', 'pineapple', 'strawberry', 'durian', 'litchi', 'pitaya']
(...)
D.clear() -> None. Remove all items from D.
删除所有项目
dict_data = {1: 1, 2: 2}
dict_data.clear()
print(dict_data)
结果
{}
(...)
D.copy() -> a shallow copy of D
浅拷贝
dict_data = {1: 1, 2: 2}
data = dict_data.copy()
print(dict_data, data)
结果
{1: 1, 2: 2} {1: 1, 2: 2}
(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)
结果
{1: 1, 2: 2} 1
(...)
D.items() -> a set-like object providing a view on D's items
一个类似集合的对象,提供字典项目的视图
dict_data = {1: 1, 2: 2, 3: 3}
data = dict_data.items()
print(dict_data, data)
结果
{1: 1, 2: 2, 3: 3} dict_items([(1, 1), (2, 2), (3, 3)])
(...)
D.keys() -> a set-like object providing a view on D's keys
一个类似集合的对象,提供 字典键上的视图
dict_data = {1: 1, 2: 2, 3: 3}
data = dict_data.keys()
print(dict_data, data)
结果
{1: 1, 2: 2, 3: 3} dict_keys([1, 2, 3])
(...)
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
删除指定的键并返回相应的值。
dict_data = {1: 1, 2: 2, 3: 4}
data = dict_data.pop(3)
print(dict_data, data)
结果
{1: 1, 2: 2} 4
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)
结果
KeyError: 4
(self, /)
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)
结果
{1: 1, 2: 2} (3, 4)
Pairs are returned in LIFO (last-in, first-out) order.
成对按后进先出(后进先出)顺序返回。
Raises KeyError if the dict is empty.
如果字典为空,则引发 KeyError。
(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: 4}
data = dict_data.setdefault(4)
print(dict_data, data)
结果
{1: 1, 2: 2, 3: 4, 4: None} None
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)
结果
{1: 1, 2: 2, 3: 4} 4
(...)
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)
结果
{1: 6, 2: 2, 3: 4, 4: 5, 5: 6}
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]
(...)
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)
结果
{1: 1, 2: 2, 3: 4} dict_values([1, 2, 4])