1.list中所有方法使用
append-给末尾添加一个元素(在原来的列表产生一个新列表)(扩展方式一)
list_data = [1, 2, 3]
list_data.append(4)
print(list_data)
D:\python\python.exe D:/python/python_code/fjq_var.py
[1, 2, 3, 4]
Process finished with exit code 0
clear-清除列表里所有元素
list_data = [1, 2, 3]
list_data.clear()
print(list_data)
D:\python\python.exe D:/python/python_code/fjq_var.py
[]
Process finished with exit code 0
count-统计给定的一个值在列表出现的次数(元组列表都是序列计数方法相同)
list_data = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 6]
print(list_data.count(2))
D:\python\python.exe D:/python/python_code/fjq_var.py
2
Process finished with exit code 0
extend-扩展(扩展方式二)
list_data = [1, 2, 3]
list_data.extend('123')
print(list_data)
list_data.extend((4, 5, 6))
print(list_data)
list_data.extend(([7, 8, 9]))
print(list_data)
list_data = [1, 2, 3]
list_data2 = [4, 5, 6]
list_data.extend(list_data2)
print(list_data)
D:\python\python.exe D:/python/python_code/fjq_var.py
[1, 2, 3, '1', '2', '3']
[1, 2, 3, '1', '2', '3', 4, 5, 6]
[1, 2, 3, '1', '2', '3', 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6]
Process finished with exit code 0
index() 函数用于从列表中找出某个值第一个匹配项的索引位置。
该方法返回查找对象的索引位置,如果没有找到对象则抛出异常。
List = ['123', 'abc', 'teacher', 'school']
print("abc索引位置: ", List.index('abc'))
print("123索引位置 : ", List.index( '123', 0, 3 ))
D:\python\python.exe D:/python/python_code/fjq_var.py
abc索引位置: 1
123索引位置 : 0
Process finished with exit code 0
insert-在指定位置放入一个值(扩展方式三)
list_data = [1, 2, 3]
list_data.insert(1, 4)
print(list_data)
list_data = [1, 2, 3]
list_data.insert(2, 4)
print(list_data)
list_data = [1, 2, 3]
list_data.insert(3, 4)
print(list_data)
D:\python\python.exe D:/python/python_code/fjq_var.py
[1, 4, 2, 3]
[1, 2, 4, 3]
[1, 2, 3, 4]
Process finished with exit code 0
pop()函数是将一个LIst里面的最后一个值取出打印返回的时候从List里面删掉。缺省值为-1即如果没有参数就取最后一个值。
list_data = [1, 2, 3]
return_value = list_data.pop()
print(list_data)
print(return_value)
D:\python\python.exe D:/python/python_code/fjq_var.py
[1, 2]
3
Process finished with exit code 0
remove函数可以删除列表中的指定元素
list_fjq = [1, 1, 2, 3]
list_fjq.remove(1)
print(list_fjq)
list_fjq = [1, 1, 2, 3]
list_fjq.remove(2)
print(list_fjq)
list_fjq = [1, 1, 2, 3]
list_fjq.remove(3)
print(list_fjq)
D:\python\python.exe D:/python/python_code/fjq_var.py
[1, 2, 3]
[1, 1, 3]
[1, 1, 2]
Process finished with exit code 0
reverse() 函数用于反向列表中元素(该方法没有返回值,但是会对列表的元素进行反向排序)
list_fjq = [1, 3, 4, 5, 2, "123", '111']
list_fjq.reverse()
print(list_fjq)
D:\python\python.exe D:/python/python_code/fjq_var.py
['111', '123', 2, 5, 4, 3, 1]
Process finished with exit code 0
sort() 函数用于对原列表进行排序
list_fjq = [1, 6, 8, 9, 4, 2, 4, 8, 0, 4, 3]
list_fjq.sort()
print(list_fjq)
D:\python\python.exe D:/python/python_code/fjq_var.py
[0, 1, 2, 3, 4, 4, 4, 6, 8, 8, 9]
Process finished with exit code 0
2. 对["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]进行默认排序
list_fjq = ["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]
list_fjq.sort()
print(list_fjq)
D:\python\python.exe D:/python/python_code/fjq_var.py
['cherry', 'durian', 'litchi', 'mangosteen', 'pineapple', 'pitaya', 'pomelo', 'strawberry']
Process finished with exit code 0
对上面的列表使用第三个字母进行排序
def sort_func(x):
return x[2]
list_fjq = ["cherry", "litchi", "strawberry", "mangosteen", "pomelo", "pineapple", "pitaya", "durian"]
list_fjq.sort(key=sort_func)
print(list_fjq)
D:\python\python.exe D:/python/python_code/fjq_var.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_fjq = {1+1: 2, 2+2: 4}
dict_fjq.clear()
print(dict_fjq)
D:\python\python.exe D:/python/python_code/fjq_var.py
{}
Process finished with exit code 0
(2)
copy(self): # real signature unknown; restored from __doc__
""" D.copy() -> a shallow copy of D """
dict_fjq = {1+1: 2, 2+2: 4}
fjq = dict_fjq.copy()
print(dict_fjq, fjq)
D:\python\python.exe D:/python/python_code/fjq_var.py
{2: 2, 4: 4} {2: 2, 4: 4}
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_fjq = {1+1: 2, 2+2: 4}
fjq = dict_fjq.get(2)
print(dict_fjq, fjq)
D:\python\python.exe D:/python/python_code/fjq_var.py
{2: 2, 4: 4} 2
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_fjq = {1+1: 2, 2+2: 4}
fjq = dict_fjq.items()
print(dict_fjq, fjq)
D:\python\python.exe D:/python/python_code/fjq_var.py
{2: 2, 4: 4} dict_items([(2, 2), (4, 4)])
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_fjq = {1+1: 2, 2+2: 4}
fjq = dict_fjq.keys()
print(dict_fjq, fjq)
D:\python\python.exe D:/python/python_code/fjq_var.py
{2: 2, 4: 4} dict_keys([2, 4])
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_fjq = {2: 2, 4: 4, 8: 8}
fjq = dict_fjq.pop(2)
print(dict_fjq, fjq)
dict_fjq = {2: 2, 4: 4, 8: 8}
fjq = dict_fjq.pop(4)
print(dict_fjq, fjq)
dict_fjq = {2: 2, 4: 4, 8: 8}
fjq = dict_fjq.pop(8)
print(dict_fjq, fjq)
D:\python\python.exe D:/python/python_code/fjq_var.py
{4: 4, 8: 8} 2
{2: 2, 8: 8} 4
{2: 2, 4: 4} 8
Process finished with exit code 0
(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_fjq = {2: 2, 4: 4, 8: 8}
fjq = dict_fjq.popitem()
print(dict_fjq, fjq)
D:\python\python.exe D:/python/python_code/fjq_var.py
{2: 2, 4: 4} (8, 8)
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_fjq = {2: 2, 4: 4, 8: 8}
fjq = dict_fjq.setdefault(8)
print(dict_fjq, fjq)
dict_fjq = {2: 2, 4: 4, 8: 8}
fjq = dict_fjq.setdefault(4)
print(dict_fjq, fjq)
dict_fjq = {2: 2, 4: 4, 8: 8}
fjq = dict_fjq.setdefault(2)
print(dict_fjq, fjq)
D:\python\python.exe D:/python/python_code/fjq_var.py
{2: 2, 4: 4, 8: 8} 8
{2: 2, 4: 4, 8: 8} 4
{2: 2, 4: 4, 8: 8} 2
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_fjq = {2: 2, 4: 4, 8: 8}
dict_fjq2 = {1: 1, 2: 2, 3: 3}
dict_fjq.update(dict_fjq2)
print(dict_fjq)
D:\python\python.exe D:/python/python_code/fjq_var.py
{2: 2, 4: 4, 8: 8, 1: 1, 3: 3}
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_fjq = {2: 2, 4: 4, 8: 8}
fjq = dict_fjq.values()
print(dict_fjq, fjq)
D:\python\python.exe D:/python/python_code/fjq_var.py
{2: 2, 4: 4, 8: 8} dict_values([2, 4, 8])
Process finished with exit code 0