1. list列表扩展的方式有几种(或者说添加元素的方法)
append追加到末尾
list_data = [1, 2, 3]
list.append(list_data, 4)
print(list_data)
E:\Python\python.exe E:/Python/NewProject_hwj/HWJ.py
[1, 2, 3, 4]
Process finished with exit code 0
extend通过迭代追加来扩展元素
list_data = [1, 2, 3]
str_data = '123'
list.extend(list_data, str_data)
print(list_data)
E:\Python\python.exe E:/Python/NewProject_hwj/HWJ.py
[1, 2, 3, '1', '2', '3']
Process finished with exit code 0
insert在索引前插入对象
list_data = [1, 2, 3]
list_data.insert(1, 4)
print(list_data)
E:\Python\python.exe E:/Python/NewProject_hwj/HWJ.py
[1, 4, 2, 3]
Process finished with exit code 0
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)
E:\Python\python.exe E:/Python/NewProject_hwj/HWJ.py
['cherry', 'durian', 'litchi', 'mangosteen', 'pineapple', 'pitaya', 'pomelo', 'strawberry']
Process finished with exit code 0
对上面的列表使用第三个字母进行排序
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)
E:\Python\python.exe E:/Python/NewProject_hwj/HWJ.py
['cherry', 'pomelo', 'mangosteen', 'pineapple', 'strawberry', 'durian', 'litchi', 'pitaya']
Process finished with exit code 0
3. dict中所有方法的使用(先写源代码再写样例)
(1)
clear(self): # real signature unknown; restored from __doc__
""" D.clear() -> None. Remove all items from D. """
dict_data = {1: 1, 2: 2}
dict_data.clear()
print(dict_data)
E:\Python\python.exe E:/Python/NewProject_hwj/HWJ.py
{}
Process finished with exit code 0
(2)
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)
E:\Python\python.exe E:/Python/NewProject_hwj/HWJ.py
{1: 1, 2: 2} {1: 1, 2: 2}
Process finished with exit code 0
(3)
get(self, *args, **kwargs): # real signature unknown
""" Return the value for key if key is in the dictionary, else default. """
dict_data = {1: 1, 2: 2}
data = dict_data.get(1)
print(dict_data, data)
E:\Python\python.exe E:/Python/NewProject_hwj/HWJ.py
{1: 1, 2: 2} 1
Process finished with exit code 0
(4)
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.items()
print(dict_data, data)
E:\Python\python.exe E:/Python/NewProject_hwj/HWJ.py
{1: 1, 2: 2, 3: 3} dict_items([(1, 1), (2, 2), (3, 3)])
Process finished with exit code 0
(5)
keys(self): # real signature unknown; restored from __doc__
""" 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)
E:\Python\python.exe E:/Python/NewProject_hwj/HWJ.py
{1: 1, 2: 2, 3: 3} dict_keys([1, 2, 3])
Process finished with exit code 0
(6)
pop(self, k, d=None): # real signature unknown; restored from __doc__
"""
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
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.pop(3)
print(dict_data, data)
E:\Python\python.exe E:/Python/NewProject_hwj/HWJ.py
{1: 1, 2: 2} 4
Process finished with exit code 0
dict_data = {1: 1, 2: 2, 3: 4}
data = dict_data.pop(4)
print(dict_data, data)
E:\Python\python.exe E:/Python/NewProject_hwj/HWJ.py
Traceback (most recent call last):
File "E:\Python\NewProject_hwj\HWJ.py", line 40, in <module>
data = dict_data.pop(4)
KeyError: 4
Process finished with exit code 1
(7)
popitem(self, *args, **kwargs): # real signature unknown
"""
Remove and return a (key, value) pair as a 2-tuple.
Pairs are returned in LIFO (last-in, first-out) order.
Raises KeyError if the dict is empty.
"""
dict_data = {1: 1, 2: 2, 3: 4}
data = dict_data.popitem()
print(dict_data, data)
E:\Python\python.exe E:/Python/NewProject_hwj/HWJ.py
{1: 1, 2: 2} (3, 4)
Process finished with exit code 0
(8)
setdefault(self, *args, **kwargs): # real signature unknown
"""
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.
"""
dict_data = {1: 1, 2: 2, 3: 4}
data = dict_data.setdefault(4)
print(dict_data, data)
E:\Python\python.exe E:/Python/NewProject_hwj/HWJ.py
{1: 1, 2: 2, 3: 4, 4: None} None
Process finished with exit code 0
dict_data = {1: 1, 2: 2, 3: 4}
data = dict_data.setdefault(3)
print(dict_data, data)
E:\Python\python.exe E:/Python/NewProject_hwj/HWJ.py
{1: 1, 2: 2, 3: 4} 4
Process finished with exit code 0
(9)
update(self, E=None, **F): # known special case of dict.update
"""
D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
In either case, this is followed by: for k in F: D[k] = F[k]
"""
dict_data = {1: 1, 2: 2, 3: 4}
dict_data1 = {1: 6, 4: 5, 5: 6}
dict_data.update(dict_data1)
print(dict_data)
E:\Python\python.exe E:/Python/NewProject_hwj/HWJ.py
{1: 6, 2: 2, 3: 4, 4: 5, 5: 6}
Process finished with exit code 0
(10)
values(self): # real signature unknown; restored from __doc__
""" D.values() -> an object providing a view on D's values """
dict_data = {1: 1, 2: 2, 3: 4}
data = dict_data.values()
print(dict_data, data)
E:\Python\python.exe E:/Python/NewProject_hwj/HWJ.py
{1: 1, 2: 2, 3: 4} dict_values([1, 2, 4])
Process finished with exit code 0